C# 如何从c代码传输zip文件
我使用的是c#.net应用程序,其中我需要使用c#代码库下载一个zip文件。我使用以下代码下载该文件:C# 如何从c代码传输zip文件,c#,asp.net,zip,download,response.transmitfile,C#,Asp.net,Zip,Download,Response.transmitfile,我使用的是c#.net应用程序,其中我需要使用c#代码库下载一个zip文件。我使用以下代码下载该文件: Response.ContentType = "application/zip" Response.AppendHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(sZipFileName))); R
Response.ContentType = "application/zip"
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(sZipFileName)));
Response.TransmitFile(sZipFilePath);
HttpContext.Current.ApplicationInstance.CompleteRequest();
zip文件已传输,但当我在下载后尝试打开zip文件时,我收到一个错误消息:“无法打开该文件:该文件看起来不是有效的存档”
请让我知道我哪里做错了,如何得到一个zip文件,并提取它没有任何错误
提前感谢尝试
Response.WriteFile(sZipFilePath)代码>相反我不确定您的代码片段为什么不能正常工作,但这里有一些代码,我正在使用它们在我的应用程序中执行相同的操作。希望能有帮助
var updateFile = new FileInfo("path/to/file");
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(updateFile.FullName) + "\"");
Response.AddHeader("content-length", updateFile.Length.ToString());
Response.TransmitFile(updateFile.FullName);
Response.Flush();
尝试:
其中ba是表示zip文件的字节数组我使用的是:
Response.ContentType = "application/zip" ;
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(path)));
Response.WriteFile(path);
Response.End();
如果变量路径是(本地)文件的完整路径,请尝试在脚本末尾添加Response.End()
。您是在服务器上压缩文件,还是下载已压缩的文件?
我认为,如果您使用的是jscript,那么返回压缩后的文件时可能会出错。
如果不是,则您没有正确返回正在压缩的文件名
我也这样做过,制作压缩文件,然后下载。
我使用了c#、jscript和asp.net
这对我来说非常有效。
我将文件传递给jscript文件,jscript调用一个方法来压缩文件,当压缩在服务器上完成时,服务器将其返回给客户端并自动开始下载。我不知道您使用的是哪台机器。。。但是Vista之前的windows版本对zip文件使用FAt32编码,Vista Microsoft开始使用NTFS作为文件格式。
请检查您的归档文件格式,因为当用户使用XP操作系统,而我在win7中创建归档文件时,我遇到了非常类似的情况(错误消息与您发布的相同)。
致以最诚挚的问候,我已经测试了流畅的代码,它运行正常,所以请分享。
我必须做的一件事是使用“Response.Flush();
”
当我添加这个时,我得到一个异常,说“ThreabAborted exception”。但这意味着将文件加载到内存中,这将无法像从磁盘流式传输文件那样扩展。您应该在调用ToString()时指定不变的区域性。(updateFile.Length.ToString(CultureInfo.InvariantCulture)
我认为这无关紧要。zip独立于创建它的文件系统:从FAT32创建的zip与从NTFS创建的zip之间唯一可能的区别是,您可以将NTFS文件权限数据作为元数据添加到中,就像存储Unix权限一样。但这些不应干扰非NTFS系统太太
Response.ContentType = "application/zip" ;
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(path)));
Response.WriteFile(path);
Response.End();
private void DownloadCandidateDocument()
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
var buffer = new byte[] {};
buffer = get you byte array here from service or file system
if (buffer.Length > 0)
{
Response.ContentType = "application/zip";
Response.BinaryWrite(buffer);
var fileName = "myzipfile";
Response.AddHeader("content-disposition",
string.Format(@"attachment;filename=""{0}""", fileName));
Response.Flush();
}
}