2013-07-04 22 views
2

我在我的網站上有一個頁面,它在服務器上動態生成PDF報告,同時向用戶瀏覽器顯示「請稍候」消息。完成後,將獨特的文件路徑放入會話中,然後打開download.aspx,它是page_load函數中以下c#代碼的空白欄。C#.NET文件下載只適用於IE

string fileUID = (string)(Session["fileUID"]); 
     string FilePath = @fileUID; 
     byte[] fileBytes = System.IO.File.ReadAllBytes(FilePath); 
     string sFileName = "Report.pdf"; 

     System.Web.HttpContext context = System.Web.HttpContext.Current; 
     context.Response.Clear(); 
     context.Response.ClearHeaders(); 
     context.Response.ClearContent(); 
     context.Response.AppendHeader("content-length", fileBytes.Length.ToString()); 
     context.Response.ContentType = "application/octet-stream"; 
     context.Response.AppendHeader("content-disposition", "attachment; filename=" + sFileName); 
     context.Response.BinaryWrite(fileBytes); 
     context.Response.Flush(); 
     System.IO.File.Delete(FilePath); 
     context.ApplicationInstance.CompleteRequest(); 

在IE瀏覽器中,會彈出允許用戶下載文件的下載對話框。然而,在鉻,火狐和Safari瀏覽器它只是坐在請等待頁永遠...

我也試過指定PDF文件的MIME類型,並沒有指定內容處置,使PDF在瀏覽器中顯示窗口,再次在Internet Explorer中完美工作,但沒有任何其他瀏覽器。

在本地主機上測試時以及上傳到服務器時都會出現此問題。

我已經在這裏和更廣泛的世界上搜索了兩個,並且似乎無法找到任何其他人有相同的問題。

請有人告訴我我的方式的錯誤。

+0

是客戶端上的等待對話框。你有代碼可以告訴我們嗎? –

+0

看看所做的實際網絡請求。發佈的代碼可能無關緊要 - 我懷疑FF/Chrome *從未在「請稍等..」之後提出二次請求。 – Paul

+0

PDF是否顯示在瀏覽器或Acrobat Reader中取決於瀏覽器。在IE中,使用acrobat插件顯示PDF文件是標準的,其他瀏覽器可能需要配置爲執行此操作。 (這是更好的方法,我**討厭**當我的PDF下載在瀏覽器中顯示。**討厭它!!! **) – Treb

回答

0

感謝您的幫助。

問題在於請等待對話頁面,我在其他瀏覽器中調試了它們,並且它們都討厭window.location(page),所以我將它更改爲window.open(page),它工作正常。

不知道我是如何錯過的。

0

這是我該怎麼辦

Response.Clear(); 
    Response.Buffer = true; 
    Response.ContentType = "application/vnd.ms-excel";//check your valid file type 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + targetFileName); 
    Response.Charset = ""; 

    using (System.IO.StreamWriter writer = new StreamWriter(Response.OutputStream) 
    { 
     //writer.Write(contentBytes); 
    } 
    Response.Flush(); 
    Response.Close(); 
    Context.ApplicationInstance.CompleteRequest(); 

確保所有的流被關閉或置於CompletedRequest()調用之前。

+0

謝謝,編號搞砸了上一頁,所以我的功能從來沒有被稱爲 – pl4gu3b0rn

0

你應該設置cookie時創建的文件這樣做:

var cookie = new HttpCookie("fileDownloadToken", _token); 
        cookie.Expires = DateTime.Now.AddMinutes(10); 
        cookie.Path = "/"; //Also set path 
        context.Response.AppendCookie(cookie); 

與jquery.cookie插件下一個頁面讀取Cookie值,如果將其設置爲預期值然後解鎖界面使用jQuery blockui:

<script type="text/javascript"> 

    var fileDownloadCheckTimer; 
    function blockUIForDownload() { 
     var token = '1357.11.22'; 
     $('#download_token_value').val(token); 
     $.blockUI({ 
      message:$('#domMessage'), 
      css: { 
       padding: 10, 
       margin: 0, 
       width: '30%', 
       top: '50%', 
       left: '35%', 

       textAlign: 'center', 
       color: '#000', 
       border: '3px solid #aaa', 
       backgroundColor: '#fff', 
       cursor: 'wait', 
      }}); 
      fileDownloadCheckTimer = window.setInterval(function() { 
       var cookieValue = $.cookie('fileDownloadToken'); 
       //alert(cookieValue); 
       if (cookieValue == token) 
        finishDownload(); 
      }, 1000); 
    } 

    function finishDownload() { 
     window.clearInterval(fileDownloadCheckTimer); 
     $.unblockUI(); 
     $.cookie('fileDownloadToken', null, { path: '/' }); 

    } 

</script>