2012-06-27 45 views
0

我寫了一個ashx處理程序,以安全的方式將文件流式傳輸到瀏覽器,我希望用戶有權訪問這些文件。文件處理程序下載中斷+會話不見了

問題是,當我蒸大文件(+ 40 MB),會話是戈南+後的瀏覽器下載突然中斷〜40 MB。

我的web.config未配置爲超時之前240分鐘。

本地測試,這並沒有給我同樣的問題,在我的共享主機做測試此。

任何人都可以將我指向正確的方向?

我試圖與不Reponse.Clear()

public void ProcessRequest(HttpContext context) 
    { 
     int id; 

     if (new Core.SecurityManager().CurrentUser != null) 
     { 

      try 
      { 
       id = Convert.ToInt32(context.Request.QueryString["id"]); 
      } 
      catch 
      { 
       throw new ApplicationException("id could not be parsed."); 
      } 

      string filename = new DocumentFactory().SelectDocumentById(id).Filename; 

      string filePath = context.Server.MapPath("~/uploads/" + filename); 

      //context.Response.Clear(); 

      context.Response.AddHeader("content-disposition", "attachment; filename=" + filename); 

      context.Response.ContentType = "application/octet-stream"; 

      context.Response.WriteFile(filePath); 

      //context.Response.Flush(); 

      //context.Response.End(); 
     } 
     else 
     { 
      throw new AuthenticationException(); 
     } 

    } 

Web.config文件:

<sessionState mode="InProc" cookieless="false" timeout="240"></sessionState> 

編輯嘗試以下,但仍然在下載中斷:

的FileStream FS =新FileStream(filePath,FileMode.Open,FileAccess.Read);

  byte[] byteArray = new byte[fs.Length]; 

      using (MemoryStream ms = new MemoryStream(byteArray)) 
      { 
       long dataLengthToRead = ms.Length; 
       int blockSize = dataLengthToRead >= 5000 ? 5000 : (int)dataLengthToRead; 
       byte[] buffer = new byte[dataLengthToRead]; 
       context.Response.Clear(); 


       // Clear the content of the response 
       context.Response.ClearContent(); 
       context.Response.ClearHeaders(); 


       // Buffer response so that page is sent 
       // after processing is complete. 
       context.Response.BufferOutput = true; 


       // Add the file name and attachment, 
       // which will force the open/cance/save dialog to show, to the header 
       context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 


       // bypass the Open/Save/Cancel dialog 
       //Response.AddHeader("Content-Disposition", "inline; filename=" + doc.FileName); 


       // Add the file size into the response header 
       context.Response.AddHeader("Content-Length", fs.Length.ToString()); 


       // Set the ContentType 
       context.Response.ContentType = "application/octet-stream"; 


       // Write the document into the response 
       while (dataLengthToRead > 0 && context.Response.IsClientConnected) 
       { 
        Int32 lengthRead = ms.Read(buffer, 0, blockSize); 
        context.Response.OutputStream.Write(buffer, 0, lengthRead); 
        //Response.Flush(); 
        dataLengthToRead = dataLengthToRead - lengthRead; 
       } 

       context.Response.Flush(); 
       context.Response.Close(); 
      } 


      // End the response 
      context.Response.End(); 

通過添加完整路徑通過瀏覽器直接進入文件時,沒有任何問題,因此不斷下載。

回答

0

雖然提供IIS中的大文件,正確的方法是以下選項,

  1. 坐落在WebLimits MinBytesPerSecond爲零(這將在提升性能肯定有所幫助,如IIS選擇關閉抱着保持活動連接的客戶端與更小的尺寸轉讓)

  2. 分配更多的工作進程的應用程序池,我已經設定爲8,現在應該這樣做只是如果你的服務器分發的文件。這肯定會導致其他網站執行速度變慢,但這將確保更好的交付。我們已經設置爲8,因爲這臺服務器只有一個網站,它只是提供巨大的文件。

  3. 關閉應用程序池回收

  4. 關閉會話

  5. 離開緩衝在

  6. 每個以下步驟之前,請檢查Response.IsClientConnected是真實的,否則放棄和不發送任何東西發送文件

  7. 地刷新響應

  8. 寫入到輸出流之前

  9. 集的Content-Length,並在定期

+0

步驟1至3沖洗是一個沒有去因爲我在共享主機上,所以限制IIS設置對我開放。第4步:我需要會話。 – Hifilover

+0

第6步到第9步我試過了,但沒有allas:查看問題更新.. – Hifilover

+0

http://stackoverflow.com/questions/7948316/how-resume-able-file-downlaod-in-asp-net-with- c-sharp-best-way-for-large-fil 訪問此鏈接。這可能會幫助你。 –