2008-12-15 67 views
2

我們在我們的asp.net網絡系統中有一個頁面,它使用response.redirect將用戶直接重定向到excel文件,因此它將自動下載而不需要用戶以右鍵點擊/另存爲。IE7在重定向到大型excel文件時出現問題

這個效果很好 - 除了IE7中約100k的文件外。其他瀏覽器只是下載大文件,而且IE在該閾值下工作正常,但在大約200k的地方,IE只是開始發佈「頁面無法顯示」錯誤。

顯然,我們希望用戶能夠在IE中下載 - 任何想法?是否有某種可以覆蓋的下載大小閾值事項?

回答

1

我更喜歡不同的發送文件的方法。它適用於各種不同類型和大小的文件。

而不是使用的Response.Redirect的,允許鏈接到該文件做,你修改響應,像這樣回傳:

Public Shared Sub SendFileToBrowser(ByRef response As HttpResponse, ByVal filepath As String, Optional ByVal filename As String = "", Optional ByVal contentType As String = "", Optional ByVal disposition As String = "", Optional ByVal contentLength As String = "") 
    Dim ext As String = filepath.Substring(filepath.Length - 3, 3) 

    If String.IsNullOrEmpty(contentType) Then 
     If ext = "pdf" Then 
      contentType = "application/pdf" 
     Else 
      contentType = "application/file" 
     End If 
    End If 

    If String.IsNullOrEmpty(disposition) Then 
     disposition = "attachment" 
    End If 

    If String.IsNullOrEmpty(filename) Then 
     ''//Test for relative url path 
     Dim fileparts As String() = filepath.Split("/") 
     If fileparts.Length > 1 Then 
      filename = fileparts(fileparts.Length - 1) 
     Else 
      ''//Test for absolute file path 
      Dim fileparts2 As String() = filepath.Split("\")  ''//" SO: Fix syntax highlighting 
      If fileparts2.Length > 1 Then 
       filename = fileparts2(fileparts2.Length - 1) 
      Else 
       ''//Just give it a temp name 
       filename = "temp." & ext 
      End If 
     End If 
    End If 

    response.Clear() 

    response.AddHeader("content-disposition", disposition & ";filename=" & filename) 
    If Not String.IsNullOrEmpty(contentLength) Then 
     response.AddHeader("Content-Length", contentLength) 
    End If 

    response.ContentType = contentType 

    response.Cache.SetCacheability(HttpCacheability.Public)   

    response.TransmitFile(filepath) 
    response.End() 
End Sub 

注意:使用「'//」徵求意見,使語法突出顯示器正常工作。這仍然編譯正確。

這適用於我們在IE6和IE7。

+0

美麗!正是我需要的。謝謝! – 2008-12-15 19:00:07