我在Chrome浏览器中将导出文件重命名为启动导出的默认页面名称时出现问题。我已经查看了我能找到的所有相关论坛帖子并尝试了所有建议 - 我在过去几年内没有看到任何近期帖子。
我的导出代码(在任何修改尝试之前)如下:
public static void ExportToSpreadsheet(object items, string name)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Context-Disposition", "attachment;filename=\"" + name + "\"");
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
{
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = items;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
以上代码在IE / Firefox中完美运行。收到的结果标头(每个Chrome net-internals)是:
Cache-Control: private
Content-Type: application/vnd.ms-excel
Server: Microsoft-IIS/8.0
Context-Disposition: attachment;filename="DataExport.xls"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcTXlfRGF0YVxBVFNNXERldjQwXHBvcnRhbFxDR0lfQXV0b21hdGlvbl9GcmFtZXdvcmtcQXV0b21hdGlvbl9EZWNrLmFzcHg=?=
X-Powered-By: ASP.NET
Date: Fri, 05 Dec 2014 19:37:05 GMT
Content-Length: 86493
我尝试过多次更新,包括将文件名硬编码为" test.xls",清除响应内容,将缓冲区设置为true,将CacheControl和Pragma设置为no-cache,以及覆盖页面代码隐藏中的VerifyRenderingInServerForm()。应用所有这些更改后的响应标头如下:
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/vnd.ms-excel
Expires: -1
Server: Microsoft-IIS/8.0
Context-Disposition: attachment; filename="test.xls"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcTXlfRGF0YVxBVFNNXERldjQwXHBvcnRhbFxDR0lfQXV0b21hdGlvbl9GcmFtZXdvcmtcQXV0b21hdGlvbl9EZWNrLmFzcHg=?=
X-Powered-By: ASP.NET
Date: Fri, 05 Dec 2014 19:10:23 GMT
Content-Length: 86493
这仍然会产生相同的结果,Chrome无法识别/接受文件名,并默认返回页面名称作为下载的文件名。
非常感谢任何有关如何解决此问题的建议。
我使用的是Chrome版本39.0.2171.71 m