2013-03-13 68 views
3

我有這個下載功能,它的工作很好。但是對於文件大小爲1.35gb的文件,下載停止在300Mb,382,400mb或1.27Gb。我究竟做錯了什麼? (下載功能是由這種方式,因爲文件需要被隱藏,並且不能在網站上公佈。)下載功能失敗,文件大小爲1.35gb

public static void downloadFunction(string filename) 
{ 
    string filepath = @"D:\texts\New folder\DLfolder\" + filename; 
    string contentType = "application/x-newton-compatible-pkg"; 

    Stream iStream = null; 
    // Buffer to read 10K bytes in chunk 
    //byte[] buffer = new Byte[10000]; 
    // Buffer to read 1024K bytes in chunk 

    byte[] buffer = new Byte[1048576]; 

    // Length of the file: 
    int length; 

    // Total bytes to read: 
    long dataToRead; 

    try 
    { 
     // Open the file. 
     iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read); 

     // Total bytes to read: 
     dataToRead = iStream.Length; 
     HttpContext.Current.Response.ContentType = contentType; 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); 
     HttpContext.Current.Response.AddHeader("Content-Length", iStream.Length.ToString()); 

     // Read the bytes. 
     while (dataToRead > 0) 
     { 
      // Verify that the client is connected. 
      if (HttpContext.Current.Response.IsClientConnected) 
      { 
       // Read the data in buffer. 
       length = iStream.Read(buffer, 0, 10000); 

       // Write the data to the current output stream. 
       HttpContext.Current.Response.OutputStream.Write(buffer, 0, length); 

       // Flush the data to the HTML output. 
       HttpContext.Current.Response.Flush(); 

       buffer = new Byte[10000]; 
       dataToRead = dataToRead - length; 
      } 
      else 
      { 
       // Prevent infinite loop if user disconnects 
       dataToRead = -1; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     // Trap the error, if any. 
     HttpContext.Current.Response.Write("Error : " + ex.Message + "<br />"); 

     HttpContext.Current.Response.ContentType = "text/html"; 
     HttpContext.Current.Response.Write("Error : file not found"); 
    } 
    finally 
    { 
     if (iStream != null) 
     { 
      //Close the file. 
      iStream.Close(); 
     } 
     HttpContext.Current.Response.End(); 
     HttpContext.Current.Response.Close(); 
    } 
} 
+0

您是否收到任何錯誤消息,異常? – 2013-03-13 11:48:29

+0

我在這裏看到一個很大的安全漏洞。假設'filename'被提供爲像「」.. \ .. \ .. \ websites \ myDomain.com \ web.config「'。 – spender 2013-03-13 11:49:23

+0

爲什麼要分配一個1MB數組,然後每次迭代分配一個〜10k數組? – 2013-03-13 11:51:15

回答

0

你可能已經打的請求超時。請注意,如果文件在經過一段預定時間後停止下載,例如60或300秒。如果是這種情況,您可以在應用程序的web.config中配置超時。

+0

無超時,將鎖住瀏覽器,從下載的隨機時間開始 – 2013-03-13 12:22:39