コード例 #1
0
        public async Task <IActionResult> Download(int id)
        {
            EducationalMaterial em = await universityData.EducationalMaterials.GetByIdAsync(id);

            List <Media>  materials = em.Materials;
            List <byte[]> files     = new List <byte[]>();

            byte[] compressed;

            foreach (var material in materials)
            {
                string path = _webHost.WebRootPath;
                files.Add(System.IO.File.ReadAllBytes(Path.Combine(path, material.GetPath())));
            }

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
                for (int i = 0; i < materials.Count; i++)
                {
                    var fileInArchive = archive.CreateEntry(materials[i].MediaName, CompressionLevel.Optimal);

                    using var entryStream          = fileInArchive.Open();
                    using var fileToCompressStream = new MemoryStream(files[i]);
                    fileToCompressStream.CopyTo(entryStream);
                }
                archive.Dispose();

                compressed = memoryStream.ToArray();
            }
            //GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
            GC.Collect(2, GCCollectionMode.Forced, false, true);

            return(File(compressed, "application/zip", em.Name + ".zip"));
        }
コード例 #2
0
 public Media(EducationalMaterial educationalMaterial, string mediaName, string contentType, string partialMediaPath, User user)
 {
     EducationalMaterial = educationalMaterial;
     ContentType         = contentType;
     MediaName           = mediaName;
     PartialMediaPath    = partialMediaPath;
     User        = user;
     CreatedDate = DateTime.Now;
 }
コード例 #3
0
        public async Task <IActionResult> AddMaterial(EducationalMaterialViewModel model, IFormFileCollection uploads)
        {
            Employee employee = await universityData.Employees.GetByFullNameAsync(model.EmployeeFullName);

            if (employee == null)
            {
                ModelState.AddModelError("FullName", "Преподаватель не найден");
            }

            if (uploads.Count <= 0)
            {
                ModelState.AddModelError("Files", "Не загржен ни один файл");
            }

            List <Media> medias = new List <Media>();


            if (ModelState.IsValid)
            {
                EducationalMaterial em = new EducationalMaterial()
                {
                    Name        = model.Name,
                    Employee    = await universityData.Employees.GetByFullNameAsync(model.EmployeeFullName),
                    Description = model.Description,
                    Subject     = model.Subject,
                    Type        = model.Type,
                    Materials   = medias
                };
                await universityData.EducationalMaterials.AddAsync(em);


                string patrialPath = "Content/EducationalMaterials/" + String.Format("{0}_{1}",
                                                                                     DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"), model.Name.Transliterate());
                string path = Path.Combine(_webHost.WebRootPath, patrialPath);
                Directory.CreateDirectory(path);
                foreach (var file in uploads)
                {
                    using (var fileSteam = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))
                    {
                        await file.CopyToAsync(fileSteam);
                    }

                    medias.Add(new Media(em,
                                         file.FileName, file.ContentType, patrialPath, await _userManager.GetUserAsync(User)));
                }

                foreach (var media in medias)
                {
                    media.EducationalMaterial = em;
                }
                foreach (var media in medias)
                {
                    await universityData.Medias.AddAsync(media);
                }
                universityData.SaveAsync();

                return(RedirectToAction("GetMaterials", "EducationalMaterials"));
            }
            ViewBag.Types = Types;
            return(View(model));
        }