gpt4 book ai didi

ASP.NET 创建 zip 文件以供下载 : the compressed zipped folder is invalid or corrupted

转载 作者:行者123 更新时间:2023-12-02 18:36:44 26 4
gpt4 key购买 nike

string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();

实际的 zip 文件 c:\temp\test.zip 是好的、有效的,无论你想怎么调用它。当我导航到目录 c:\temp\并双击 test.zip 文件时;它会立即打开。

我的问题似乎只是与下载有关。上面的代码执行没有任何问题。出现一个文件下载对话框。我可以选择保存或打开。如果我尝试从对话框中打开文件,或者保存它然后打开它。我收到以下对话框消息:

压缩的文件夹无效或已损坏。

对于 Response.ContentType 我尝试过:

应用程序/x-压缩应用程序/x-zip-压缩应用程序/x-gzip-压缩应用程序/八位字节流应用程序/zip

使用一些先前的代码创建 zip 文件(由于我能够直接打开创建的文件,我确信该代码工作正常):Ionic.zip

http://www.codeplex.com/DotNetZip

最佳答案

这有效。我不知道为什么,但确实如此。

string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);

Response.Clear();
//Response.ClearContent();
//Response.ClearHeaders();
//Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.AppendHeader("Content-Cength", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
//Response.Flush();
Response.End();

关于ASP.NET 创建 zip 文件以供下载 : the compressed zipped folder is invalid or corrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2456842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com