ASP.NET MVC:下载excel文件

Pom*_*air 6 asp.net-mvc excel asp.net-mvc-3

我正在C#动作方法中下载一个excel文件来重新编译FileResult,如下所示:

return File(bindata, "application/octet-stream", "mytestfile.xls");
Run Code Online (Sandbox Code Playgroud)

当我手动导航到与上述方法对应的URL时,我将获得该文件的渲染表示.该文件不会使用另存为 - 对话框下载.

有没有办法通过Save As -dialog强制下载?

-pom-

Dar*_*rov 8

通常,当您为File方法指定文件名时,它会自动附加Content-Disposition标题,以便始终显示"另存为"对话框.当你说你的代码不起作用时,我有点惊讶.您还可以尝试手动设置此标头:

Response.AppendHeader("Content-Disposition", "attachment; filename=mytestfile.xls");
Run Code Online (Sandbox Code Playgroud)


nik*_*d23 5

由于您要返回的媒体类型,我感觉您正在获得此行为.

尝试将媒体类型更改为application/vnd.ms-excel,如下所示:

return File(bindata, "application/vnd.ms-excel", "mytestfile.xls");
Run Code Online (Sandbox Code Playgroud)