2012-08-07 42 views
2

我試圖在瀏覽器中使用BinaryWriter打開一個文件。這會導致打開一個對話窗口並提示用戶保存或打開文件。這種行爲是好的;但是,如果我選擇打開,則會再次調用aspx頁面,經過漫長的等待後,文件最終打開。Response.BinaryWrite調用ASPX頁面兩次

我設置的ContentType

Response.BinaryWrite(binary); 
Response.End(); 
Repsonse.Close(); 

此行爲僅與Excel和Word文件時出現。

瀏覽器IE8

+1

哪種瀏覽器?你設置了哪些內容類型? – 2012-08-07 20:43:30

+0

這似乎完全在瀏覽器端。如果您選擇打開,似乎瀏覽器正在重新請求您的網址。如果這是瀏覽器正在做的事情,那麼你無能爲力。 – 2012-08-07 21:06:43

+0

@PeterRitchie你似乎是正確的。 MS Office應用程序執行第二個請求;但是,pdf和其他類型不。 – 2012-08-08 17:10:48

回答

1

替代的方法。你可以檢查出來:

FileInfo fileInfo = new FileInfo(yourFilePath);  
context.Response.Clear(); 
context.Response.ContentType = contentType; //Set the contentType 
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath)); 
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
context.Response.TransmitFile(yourFilePath); 

希望這將有助於