1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public IActionResult DownloadDocumentsAsZip() { //get the source files List<byte[]> documents = new List<byte[]>(); // the output bytes of the zip byte[] fileBytes = null; // create a working memory stream using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { // create a zip using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true)) { // iterate through the source files foreach (var f in documents) { // add the item name to the zip System.IO.Compression.ZipArchiveEntry zipItem = zip.CreateEntry("entryName"); // add the item bytes to the zip entry by opening the original file and copying the bytes using (System.IO.MemoryStream originalFileMemoryStream = new System.IO.MemoryStream(f)) { using (System.IO.Stream entryStream = zipItem.Open()) { originalFileMemoryStream.CopyTo(entryStream); } } } } fileBytes = memoryStream.ToArray(); } // download the constructed zip return File(fileBytes.ToArray(), "application/zip", "AllFiles.zip"); } |
.NET Core MemoryStream’den Dosyaları .zip olarak indirme
Tarih:.NET Core
İlk Yorumu Siz Yapın