How to force Chrome to open an “open file dialog”

2019-01-25 11:34发布

I have a page that sends a binari file, pdf, word or excel to web browser. In firefox, and IE both opens a dialog asking what do you whant to do with this file, "open" or "save"

enter image description here

but Chrome directly save it to your computer.

Is it possible to make Chrome ask you what do you want to do with this file, placing some metadata into web response before sending the file to browser?

3条回答
趁早两清
2楼-- · 2019-01-25 12:05

It isn't possible to force Chrome to prompt the save dialog. The user has to change that behavior on chrome's configuration (advanced settings).

enter image description here

查看更多
The star\"
3楼-- · 2019-01-25 12:06

You need to send some headers so the browser knows how to handle the thing you are streaming like:

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

which will force a download. Also refer this link for more information :

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

Thanks

查看更多
▲ chillily
4楼-- · 2019-01-25 12:11

maybe it will be helpful

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();
查看更多
登录 后发表回答