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

2019-01-25 11:46发布

问题:

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"

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?

回答1:

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).



回答2:

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



回答3:

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