有誰知道爲什麼返回一個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 ')';
這個想法是在控制器中創建一個方法,該方法將帶有特定代碼的表格帶有複選框的視圖中,並且我將查找數據庫記錄中的信息,每個代碼都有一個附件,保存在sql,就像varbinary一樣。然後對於每一個,這個想法是創建一個zip文件,並能夠下載它。謝謝! –