Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用“另存为”对话框压缩文件并下载它们_Asp.net - Fatal编程技术网

Asp.net 使用“另存为”对话框压缩文件并下载它们

Asp.net 使用“另存为”对话框压缩文件并下载它们,asp.net,Asp.net,我有以下代码来压缩所有文件,然后将其保存到硬盘。我想压缩所有文件(这已经完成),然后将压缩文件附加到响应流,以便用户可以选择保存它 protected void DownloadSelectedFiles_Click(object sender, EventArgs e) { string path = String.Empty; string zipFilePath = @"C:\MyZipFile.zip";

我有以下代码来压缩所有文件,然后将其保存到硬盘。我想压缩所有文件(这已经完成),然后将压缩文件附加到响应流,以便用户可以选择保存它

protected void DownloadSelectedFiles_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            string zipFilePath = @"C:\MyZipFile.zip";

            ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
            byte[] buffer = new byte[4096];

            foreach (GridViewRow row in gvFiles.Rows)
            {
                bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;

                if (isSelected)
                {
                    path = (row.FindControl("lblUrl") as Label).Text;

                    ZipEntry entry = new ZipEntry(Path.GetFileName(path));
                    entry.DateTime = DateTime.Now;

                    zipStream.PutNextEntry(entry);

                    using (FileStream fs = File.OpenRead(path))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
            }


            zipStream.Finish();
            zipStream.Close();

            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
            Response.WriteFile(zipFilePath);
            Response.End();

        }

如果你正在使用IE,请检查它是否不是旧的“缓存已满”——显示其丑陋面孔的bug

如果你将IE设置为自动刷新现金,或者在IE开始时,下载了第一次损坏的zip文件,那么可能是在你修复了你的例程并获得了一个好的zip后,它使用了该zip文件的缓存版本

尝试添加:

  Response.Buffer = true;
  Response.Clear();
  Response.ClearContent();
  Response.ClearHeaders();
在响应之前。ContentType 并加上:

Response.Flush()
Response.Close()
在response.end之前,查看是否有任何变化

结果是:

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-zip-compressed";  
Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");  
Response.WriteFile(zipFilePath);  
Response.Flush()
Response.Close()
Response.End();  

这只是一些发自内心的提示。

还有一个很长的问题,请尝试将mime类型设置为application/unknown,在这篇文章中,似乎有部分解决问题的方法:


我还尝试将zip文件保存到服务器文件夹,然后给用户一个下载链接,但它显示相同的“文件已损坏”。这是非常奇怪的,因为我可以打开相同的文件夹,如果我访问我的服务器文件夹,并手动打开它

这是我发现的另一件事。如果我将zip文件保存在服务器的文件夹中,然后使用url引用它:

如果我转到url并下载zip文件,我会得到相同的错误“文件已损坏”。但是,如果我使用文件资源管理器手动转到文件夹并打开该文件,那么它将按预期工作


为什么和如何

我不久前在博客上写过这样的文件。你可能会发现里面有些有用的东西


每次我下载ZIP文件时,它都已损坏!磁盘上的zip文件是否也已损坏?没有zip文件未损坏。我有3个文本文件,我把它们创建成一个zip文件,然后下载,但它说损坏了。如果我把它保存在硬盘上,那么它就可以打开了!我也试过同样的结果。zip文件已损坏!是否尝试使用其他浏览器?当其他浏览器出现这个问题时,我在不同的浏览器上看到了不同的结果。如果是IE,清除现金,再试一次。