我有一個頁面發送binari文件,pdf,word或excel到網頁瀏覽器。在Firefox和IE都打開一個對話框,詢問你怎麼whant做這個文件,「打開」或「保存」如何在下載文件時強制Chrome打開「打開文件對話框」víaASP.NET代碼隱藏?
但鉻直接將其保存到您的計算機。
是否有可能讓使Chrome瀏覽器詢問您對此文件做什麼,在將文件發送到瀏覽器之前將一些元數據放入Web響應中?
我有一個頁面發送binari文件,pdf,word或excel到網頁瀏覽器。在Firefox和IE都打開一個對話框,詢問你怎麼whant做這個文件,「打開」或「保存」如何在下載文件時強制Chrome打開「打開文件對話框」víaASP.NET代碼隱藏?
但鉻直接將其保存到您的計算機。
是否有可能讓使Chrome瀏覽器詢問您對此文件做什麼,在將文件發送到瀏覽器之前將一些元數據放入Web響應中?
這是不可能迫使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();
該設置會在哪裏? –
@ValentinDespa更新了答案。 – daniloquio
但要求用戶勾選此複選框並不現實,那麼是否還有其他解決方案? –