我的ASP.NET MVC Web应用程序中有一个简单的文件浏览器。
当用户想要获取文件时,他单击链接,服务器将返回文件。
JS函数:
function downloadFiles(files){
if (Array.isArray(files)) {
$.each(files, function (index, value) {
window.open('@Url.Action("Download","Documents")?file=' + encodeURIComponent(value));
});
}
else if (typeof files === "string") {
window.open('@Url.Action("Download","Documents")?file=' + encodeURIComponent(files));
}
}
参数file
包含文件的完整路径:
public FileResult Download()
{
string file = Request["file"];
return File(file, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(file));
}
当我使用IE下载文件并要求打开或另存为时,如果我选择“打开”,则Excel表示文件已损坏。如果我保存文件,则可以正常工作。
我尝试添加其他Response
标头,但随后收到无效的响应错误:
Response.AddHeader("Content-Disposition", "attachment;filename=\""+ Path.GetFileName(file)+"\"");
当我打开.pdf
或.docx
文件时,不会出现此问题。
我在做什么错了?