【发布时间】:2012-12-05 16:36:56
【问题描述】:
我有一个向网络浏览器发送二进制文件、pdf、word 或 excel 的页面。在 Firefox 和 IE 中都会打开一个对话框,询问您要如何处理此文件,“打开”或“保存”
但是Chrome直接保存到你的电脑上。
是否可以让 Chrome 询问您要如何处理此文件,在将文件发送到浏览器之前将一些元数据放入 Web 响应中?
【问题讨论】:
标签: c# asp.net google-chrome httpresponse
我有一个向网络浏览器发送二进制文件、pdf、word 或 excel 的页面。在 Firefox 和 IE 中都会打开一个对话框,询问您要如何处理此文件,“打开”或“保存”
但是Chrome直接保存到你的电脑上。
是否可以让 Chrome 询问您要如何处理此文件,在将文件发送到浏览器之前将一些元数据放入 Web 响应中?
【问题讨论】:
标签: c# asp.net google-chrome httpresponse
无法强制 Chrome 提示保存对话框。用户必须在 chrome 的配置上更改该行为(高级设置)。
【讨论】:
也许会有所帮助
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();
【讨论】:
您需要发送一些标头,以便浏览器知道如何处理您正在流式传输的内容:
Response.ContentType
Content-Disposition, application,and
filename=" + FileName
这将强制下载。另请参阅此链接以获取更多信息:
http://www.devtoolshed.com/aspnet-download-file-web-browser
谢谢
【讨论】: