2008-12-17 49 views
0

由于某些原因,当我从我的服务器的文件夹下载zip文件夹时,它总是被损坏。下面是代码:下载后邮编文件夹总是损坏

protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
     { 
      string fileUrl = String.Empty; 

      if(e.CommandName.Equals("DownloadFile")) 
      { 
       fileUrl = e.CommandArgument as String; 
       string fileName = Path.GetFileName(fileUrl); 

       Response.AppendHeader("content-disposition", 
     "attachment; filename=" + fileName); 
       Response.ContentType = "application/zip"; 

       Response.WriteFile(fileUrl); 
       Response.End(); 



      } 
     } 

这里是GridView控件的填充方式:

private void BindData() 
     { 
      List<SampleFile> files = new List<SampleFile>(); 

      for(int i=1;i<=3;i++) 
      { 
       SampleFile sampleFile = new SampleFile(); 
       sampleFile.Name = "File " + i; 
       sampleFile.Url = Server.MapPath("~/Files/File"+i+".txt"); 
       files.Add(sampleFile); 
      } 

      SampleFile file = new SampleFile(); 
      file.Name = "Zip File"; 
      file.Url = Server.MapPath("~/Files/WebSiteNestedMasters.zip"); 
      files.Add(file); 
      gvFiles.DataSource = files; 
      gvFiles.DataBind(); 
     } 

回答

0

我只是想与.docx文件和相同的结果(该文件已损坏)相同的代码:以下是代码:

if(e.CommandName.Equals("DownloadFile")) 
      { 
       fileUrl = e.CommandArgument as String; 
       string fileName = Path.GetFileName(fileUrl); 

       FileInfo info = new FileInfo(fileUrl); 

       Response.Clear(); 
       Response.ClearContent(); 
       Response.ClearHeaders(); 
       Response.Buffer = true; 

       Response.AppendHeader("Content-Length",info.Length.ToString()); 
       Response.ContentType = GetContentType(fileUrl); 
       Response.AppendHeader("Content-Disposition:", "attachment; filename=" + fileName); 
       Response.TransmitFile(fileUrl); 
       Response.Flush(); 
       Response.End(); 
      } 
1

我不知道asp.net可言,但是这是很常见的做下载的结果在文本模式而不是二进制模式下。换行结尾的字符会从\ r \ n转换为\ n,反之亦然,并且所有事情都会发生。

+0

那么,我们如何解决这个问题?你可以解释吗! – azamsharp 2008-12-17 21:01:26

0

首先,验证文件的内容是否看起来像一个zip文件。只需将文件放入记事本并查看内容即可。如果文件以PK和一些有趣的字符开头,则可能是一个zip文件。如果它包含HTML,那可能是它为什么会出错的线索。

看着你的代码,它让我感到你将URL传递给了文件Response.WriteFile。 Response.WriteFile处理url的还是使用本地文件名?也许一切都由Response.WriteFile起作用,因此正确的头文件被发送等等,但是随后的代码会崩溃,这可能会让你的“zip文件”包含一个HTML错误信息。


编辑:好吧,先解决问题的步骤完成。文件看起来像记事本中的zip文件,而不是HTML错误消息。

下一步,由于文件驻留在服务器上,因此请尝试将文件直接写入文件,而不是通过应用程序。那样有用吗?

如果没有,请尝试使用不同的zip程序(您使用哪种方式?)。

作为一个例子,你应该直接尝试文件和不同的解压缩程序。 FinalBuilder会生成PowerArchiver抱怨的zip文件,但7zip不会,所以文件或程序可能有问题,甚至可能两者都有问题。

但是请验证文件是否正确,可以打开,如果您完全不下载它们,并检查如果将它们直接下载到应用程序之外会发生什么情况。


编辑:好吧,你已经验证,即使你直接从你的服务器下载该文件将不会打开。

如何在浏览器中打开文件,完全绕开Web服务器?例如,由于它看起来像您在本地有文件,因此请尝试导航到正确的文件夹并直接打开文件。那样有用吗?

+0

如果我使用相同的技术下载文本文件,那么它将完美地打开文本文件。但是,当我使用zip文件它说:“压缩zip文件夹无效或损坏”我想我需要发送额外的响应标题,但什么? – azamsharp 2008-12-17 21:06:08

+0

您是否按照我的建议将文件拖入记事本中? – 2008-12-17 21:07:34

+0

只是我刚刚检查,它开始于以下内容: PK W9 WebSiteNestedMasters/App_Data/PK4Ÿ9^〜A}®+ – azamsharp 2008-12-17 21:08:51

1

由于@lassevk建议您下载损坏的 zip文件并将其与服务器上的原始文件进行比较。都是一样的长度?使用十六进制编辑器检查文件的内容。在这个related thread你说如果你直接将浏览器指向zip文件,它也会被破坏,这意味着问题可能与头文件无关,但是IIS有问题。您是否在使用可修改文件的第三方ISAPI扩展?

0

下面是更新后的代码:

fileUrl = e.CommandArgument as String; 
       string fileName = Path.GetFileName(fileUrl); 

       FileStream fs = new FileStream(fileUrl, FileMode.Open); 
       byte[] buffer = new byte[fs.Length]; 
       fs.Read(buffer, 0, (int) fs.Length); 
       fs.Close(); 

       Response.Clear(); 
       Response.AppendHeader("content-disposition", 
     "attachment; filename=" + fileName); 
       Response.ContentType = "application/octet-stream"; 
       Response.AppendHeader("content-length", buffer.Length.ToString()); 

       Response.BinaryWrite(buffer); 
       Response.Flush(); 
       Response.End(); 
0

这里是小提琴手统计:

HTTP/1.1 200 OK 服务器:ASP.NET开发服务器/ 8.0.0.0 日期:周三,17 Dec 2008 22:22:05 GMT X-AspNet-Version:2.0.50727 Content-Disposition:attachment;文件名= MyZipFolder.zip 的Content-Length:148 缓存控制:私人 内容类型:应用程序/ x-ZIP压缩 连接:关闭

1

假设你需要这样做:(例如,你不能以提供直接链接)

<a href='<% Eval("Url")) %>'>download</a> 

我会首先做一个观察,即让RowCommand处理程序开始返回文件不是这样做的。创建某种类型的下载链接到不同的页面。从包含网格的页面的其余部分单独下载文件。

现在......

你已经得到了有关的权利的基础,但像在this CodeProject tutorial评论你很快会遇到的问题。

Where the above code sample falls down:

  • not responsive to user
  • the code will keep going and you'll have no idea if the user actually downloaded the file (it may not matter to you)
  • won't work with all browsers
  • cannot resume a download
  • doesn't show progress
  • larger files will use more server memory and take longer to stream
  • and a lot of downloads will mean the server is going to take a resource hit

,而你可能想..

  • works just like a clicked download (ie using get, not post)
  • works in all browsers on all platforms in the way the user expects (ie filename hint works on things like IE for Mac or Netscape 4.1).
  • show download progress
  • resumable downloads
  • tracking all downloads and knowing when downloads complete
  • looks like a file, even if it isn't
  • expiration on url.
  • allows for high concurrent # of downloads for any size of file

虽然用VB编写的.net1.1文章Tracking and Resuming Large File Downloads in ASP.NET [devx]远不如在解释如何正确地做到这一点。

简单的代码很简单,但并不总是奏效,并且有效地将文件流式传输给用户100%的时间比设置内容头部和铲除网络上的某些位花费更多的精力。