2017-03-08 60 views
-1

有誰知道爲什麼返回一個zip文件,然後打開其中包含的文件無法打開,並告訴我文件已損壞,某人理解以下方法並檢測到錯誤? 非常感謝誰能夠貢獻,這是在我的控制器的方法:將文件添加到zip並下載MVC5

Public FileResult Download() 
     { 
      List <String> codes = new List <string>(); 
      Codes.Add ("1079"); 
      Codes.Add ("1078"); 
      Codes.Add ("1077"); 
      MemoryStream ms = new MemoryStream(); ; 
      Using (var zip = new ZipArchive (ms, ZipArchiveMode.Create, true)) 
      { 
       Foreach (string codigoGar in codes) 
       { 
        String mimetypeOfFile = ""; 
        Guarantees oGarantia = ControllerGarantias.getGarantia (SessionHelper.GetEntorno(), codeGar); 
        Var stream = new MemoryStream (oGarantia.comprobante); 


        Byte [] buffer = new byte [1024]; 
        If (stream.Length> = 1024) 
         Stream.Read (buffer, 0, 1024); 
        Else 
         Stream.Read (buffer, 0, (int) stream.Length); 

        Try 
        { 
         System.UInt32 mimetype; 
         FindMimeFromData (0, null, buffer, 1024, null, 0, out mimetype, 0); 
         System.IntPtr mimeTypePtr = new IntPtr (mimetype); 
         MimetypeOfFile = Marshal.PtrToStringUni (mimeTypePtr); 
         Marshal.FreeCoTaskMem (mimeTypePtr); 

         String fileName = ""; 

         If (! String.IsNullOrEmpty (mimetypeOfFile)) 
         { 
          Switch (mimetypeOfFile.ToLower()) 
          { 
           Case "application/pdf": 
            FileName = "Voucher_" + oGarantia.ServiceName + "_" + oGarantia.nroFactura + ".pdf"; 
            Break; 
           Case "image/x-png": 
            FileName = "Voucher_" + oGarantia.ServiceName + "_" + oGarantia.nroFactura + ".png"; 
            Break; 
           Case "image/pjpeg": 
            FileName = "Voucher_" + oGarantia.ServiceName + "_" + oGarantia.nroFactura + ".jpg"; 
            Break; 
          } 
         } 
         Var entry = zip.CreateEntry (fileName, CompressionLevel.Fastest); 

         Using (MemoryStream fileStream = stream) 
         Using (var entryStream = entry.Open()) 
         { 
          FileStream.CopyTo (entryStream); 
         } 
        } 
        Catch (Exception e) 
        { 
         Return null; 
        } 
       } 
      } 
      Return File (ms.ToArray(), "application/zip", "VouchersGuarantees.zip"); 
     } 

並把它從JavaScript調用,我用的是以下行:

Window.location.href = '@ Url.Action ("Download",' Warranties ')'; 
+0

這個想法是在控制器中創建一個方法,該方法將帶有特定代碼的表格帶有複選框的視圖中,並且我將查找數據庫記錄中的信息,每個代碼都有一個附件,保存在sql,就像varbinary一樣。然後對於每一個,這個想法是創建一個zip文件,並能夠下載它。謝謝! –

回答

0

我會給你一個示例代碼該zip文件和工作,你可能會適應你的代碼。

首先,您需要安裝DotNetZip庫。

public FileResult Download() 
     { 
      // using DotNetZip library 
      using (ZipFile zip = new ZipFile()) 
      { 
       // your files (or you can just pull the byte arrays and filenames from your database) 
       var file1 = Server.MapPath("~/archivosDescargar/ejemplo 1.pdf"); 
       var file2 = Server.MapPath("~/archivosDescargar/ejemplo 2.pdf"); 
       var file3 = Server.MapPath("~/archivosDescargar/1080.png"); 

       var fileName1 = Path.GetFileName(file1); 
       var byteArray1 = System.IO.File.ReadAllBytes(file1); 
       var fileName2 = Path.GetFileName(file2); 
       var byteArray2 = System.IO.File.ReadAllBytes(file2); 
       var fileName3 = Path.GetFileName(file3); 
       var byteArray3 = System.IO.File.ReadAllBytes(file3); 

       // This adds each file to the zip 
       zip.AddEntry(fileName1, byteArray1); 
       zip.AddEntry(fileName2, byteArray2); 
       zip.AddEntry(fileName3, byteArray3); 

       using (MemoryStream output = new MemoryStream()) 
       { 
        zip.Save(output); 
        return File(output.ToArray(), "application/zip", "sample9.zip"); 
       } 
      } 
     } 
+0

謝謝FelipeGavilàn。我會嘗試與這些圖書館,然後將評論你! –

+0

我有什麼不幸,我得到了同樣的結果,我不明白髮生了什麼,無論如何,我會繼續尋找。 –

+0

您是否在我的代碼中向您展示了一個目錄中的pdf的簡單示例? –

0

好吧,我終於找到了錯誤,這是我創建壞拉鍊條目。我是用內存流做以下列方式:

zip.AddEntry(fileName, stream); 

當它應該是如下,字節[]:

zip.AddEntry(fileName, oGarantia.comprobante); 

你的榜樣費利佩·加維蘭對我幫助很大,非常感謝爲所有的支持。