由于文件损坏,无法打开由ASP.NET MVC服务的xlsx文件

时间:2018-07-20 06:31:19

标签: c# jquery asp.net-mvc file download

我的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文件时,不会出现此问题。

我在做什么错了?

0 个答案:

没有答案