簡體   English   中英

通過 ASP.NET 代碼隱藏下載文件時,如何強制 Chrome 打開“打開文件對話框”?

[英]How to force Chrome to open an "open file dialog" when downloading a file vía ASP.NET codebehind?

我有一個頁面可以將二進制文件、pdf、word 或 excel 發送到網絡瀏覽器。 在 Firefox 和 IE 中,都會打開一個對話框,詢問您要對這個文件做什么,“打開”還是“保存”

在此處輸入圖片說明

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

是否可以讓 Chrome 詢問您想對這個文件做什么,在將文件發送到瀏覽器之前將一些元數據放入網絡響應中?

無法強制 Chrome 提示保存對話框。 用戶必須在 chrome 的配置(高級設置)上更改該行為。

在此處輸入圖片說明

您需要發送一些標頭,以便瀏覽器知道如何處理您正在流式傳輸的內容,例如:

Response.ContentType 
Content-Disposition, application,and 
filename=" + FileName

這將強制下載。 另請參閱此鏈接以獲取更多信息:

http://www.devtoolshed.com/aspnet-download-file-web-browser

謝謝

也許它會有所幫助

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM