将文件添加到zip并下载MVC5

时间:2017-03-08 20:53:57

标签: javascript asp.net-mvc

有谁知道为什么返回一个zip文件,然后打开其中包含的文件,无法打开,并告诉我文件已损坏,有人理解以下方法并检测到错误? 非常感谢谁可以贡献,这是我的控制器中的方法:

 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 ')';

2 个答案:

答案 0 :(得分:0)

我将为您提供一个压缩文件和工作的示例代码,您可以调整代码。

首先,您需要安装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");
                }
            }
        }

答案 1 :(得分:0)

好吧,我终于找到了错误,而且是我创建了错误的zip条目。我正在以下面的方式使用内存流:

zip.AddEntry(fileName, stream);

当它应该如下,用byte []:

zip.AddEntry(fileName, oGarantia.comprobante);

你的榜样FelipeGavilán给了我很多帮助,非常感谢所有的支持。