【发布时间】:2012-04-05 02:40:27
【问题描述】:
我正在尝试使用发送 xlsx 文件
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/vnd-ms.excel";
Response.TransmitFile(file.FullName);
Response.End();
弹出IE对话框,我可以成功保存文件,然后从文件夹中打开它,效果很好。但是如果我在 IE 对话框中单击“打开”,我会得到“无法下载 myFile.xlsx”。我单击“重试”,它会打开 Excel,但会弹出“Excel 无法打开文件 'myFile.xlsx',因为文件格式或文件扩展名无效...”错误。
我目前正在以调试模式从 VS2010 运行该站点。
有人知道为什么它会让我保存,但不能直接打开吗?
编辑
Chrome 只是下载它。 FF 尝试打开它但给出错误The file you are trying to open, 'myFile.xlsx.xls', is in a different format than specified by the file extension... 我可以选择打开它并且它成功打开,但处于只读模式。
所以,这里发生了一些时髦的事情。
文件名 = "myFile.xlsx"
编辑 2
这是在 IE 9 中。我还尝试将 octet-stream 和 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 作为 ContentType。
【问题讨论】:
-
你在FF或Chrome中试过了吗?这绝对令人困惑,因为 IE 应该只是将您的文件保存到临时位置并从那里打开它。
标签: c# asp.net response.transmitfile