2012-12-18 51 views
8

我有一個頁面發送binari文件,pdf,word或excel到網頁瀏覽器。在Firefox和IE都打開一個對話框,詢問你怎麼whant做這個文件,「打開」或「保存」如何在下載文件時強制Chrome打開「打開文件對話框」víaASP.NET代碼隱藏?

enter image description here

直接將其保存到您的計算機。

是否有可能讓使Chrome瀏覽器詢問您對此文件做什麼,在將文件發送到瀏覽器之前將一些元數據放入Web響應中?

回答

9

這是不可能迫使Chrome瀏覽器提示保存對話框。用戶必須在鉻配置(高級設置)上更改該行爲。

enter image description here

+0

該設置會在哪裏? –

+0

@ValentinDespa更新了答案。 – daniloquio

+0

但要求用戶勾選此複選框並不現實,那麼是否還有其他解決方案? –

0

也許這將是有益的

System.String filePath = "c:\\tempFile.pdf" 
System.IO.FileInfo fileInfo = new FileInfo(filePath); 

System.Web.HttpContext context = System.Web.HttpContext.Current; 
System.Web.HttpResponse response = context.Response; 
response.Clear(); 
response.ClearHeaders(); 
response.ClearContent(); 
response.ContentType = "application/pdf"; 
response.AppendHeader("content-type", "application/pdf"); 
response.AppendHeader("content-length", fileInfo.Length.ToString()); 
response.AppendHeader("content-disposition", String.Format("attachment; filename={0}.pdf", outputFileName)); 
response.TransmitFile(filePath); 
response.Flush(); // this make stream and without it open chrome save dialog 
context.ApplicationInstance.CompleteRequest(); // send headers and c# server-side code still continue 

System.IO.File.Delete(filePath); // clean cache here if you need 

response.End(); 
相關問題