2011-11-16 46 views
1

我在ASP.NET中创建了一个列出指定文件夹中的文件的脚本。无法在ASP.NET中保存较大的下载文件

protected void Page_Load(object sender, EventArgs e) { 

     //get file list 
     DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/download")); 
     int i = 0; 
     foreach (FileInfo fi in di.GetFiles()) { 
      HyperLink link = new HyperLink(); 
      link.ID = "HyperLink" + (i++); 
      link.ToolTip = fi.Length.ToString(); 
      link.Text = fi.Name; 
      link.NavigateUrl = "downloading.aspx?file=" + fi.Name; 
      Page.Controls.Add(link); 
      Page.Controls.Add(new LiteralControl("<br/>")); 
     } 
     } 

downloading.aspx文件,我有:

protected void Page_Load(object sender, EventArgs e) { 
     string filename = Request["file"].ToString(); 
     FileDownload(filename, Server.MapPath("~/download/" + filename)); 
     } 

     /// <summary> 
     /// Download specified file and his url 
     /// </summary> 
     /// <param name="filename"></param> 
     /// <param name="fileUrl"></param> 
     private void FileDownload(string filename, string fileUrl) { 
     Page.Response.Buffer = true; 
     Page.Response.Clear(); 
     DownloadFile df = new DownloadFile(); 
     bool success = df.DownloadIt(Page.Response, filename, fileUrl); 
     if (!success) { 
      Response.Write("Download error"); 
     } 
     Page.Response.End(); 
     } 

,并定义的DownloadIt方法:

public bool DownloadIt(HttpResponse response, string filename, string fileUrl) { 

     if (response == null) { 
      throw new Exception(); 
     } 

     if (string.IsNullOrEmpty(filename)) { 
      throw new ArgumentNullException("filename"); 
     } 

     try { 
      response.ContentType = "application/octet-stream"; 
      response.AppendHeader("Content-Disposition", "attachment; filename=" + filename); 
      response.TransmitFile(fileUrl); 
     } catch { 
      return false; 
     } 
     return true; 
     } 

在默认页面列表中,我有一个具有5-文件6 MB,当我点击它,对话框浏览器出现(与'保存''打开''取消'按钮),但有一个字段的文件大小为14个字节....

That DXF file has 5MB or over but there is 14 bytes

为什么?

其他小文件,大小显示正确。

我和流缓冲区有关吗?

我解决了用下面的代码问题

try { 
      FileInfo fileDownload = new FileInfo(fileUrl); 
      int bufferSize = 0; 
      bufferSize = (int)fileDownload.Length; 
      using (Stream s = new FileStream(fileUrl, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize)) { 
       byte[] buffer = new byte[bufferSize]; 
       int count = 0; 
       int offset = 0; 
       response.ContentType = "application/octet-stream"; 
       response.AddHeader("Content-Length", buffer.Length.ToString()); 
       response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 
       while ((count = s.Read(buffer, offset, buffer.Length)) > 0) { 
        response.OutputStream.Write(buffer, offset, count); 
       } 
       bufferSize = 0; 
      } 
     } catch { 
      return false; 
     } 

现在的问题变成了具有> 200 MB所致bufferSize这是int和它是FileStream右参数文件。

+2

你意识到你的代码允许别人从你的服务器下载任何文件,同时,对不对? –

+0

如果业务需求需要某种协作环境来共享文件等,为什么不考虑SharePoint? –

+0

/downloading.aspx?file=../web.config(可能需要进行网址编码)只是fyi。 –

回答

2

尝试,

byte []bytes=System.IO.File.ReadAllBytes(fileUrl); 
response.Clear(); 
response.ClearHeaders(); 
response.AddHeader("Content-Type","application/octet-stream"); 
response.AddHeader("Content-Length",bytes.Length.ToString()); 
response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 
response.BinaryWrite(bytes); 
response.Flush(); 
response.End(); 
+0

return'下载错误' –