将MVC app .xlsx文件作为类型文件下载

问题描述:

我正在研究MVC应用程序,并试图链接到我们的用户可以下载的文件共享上的Excel文件。我以为我把它设置正确,但是当我下载文件时,它会返回为'文件'类型。将MVC app .xlsx文件作为类型文件下载

UI代码

@Html.ActionLink("Excel sheet", "Download", new { @class = "a" }) 

控制器代码

public FileResult Download() 
    { 
     var FilePath = CurrentSettings.FilePath + "Template Files\\Entry Template.xlsx"; 

     return File(FilePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", "Specimen Template File"); 
    } 

,这里是它是如何在Windows资源管理器显示出来。

enter image description here

觉得这是显而易见的事情,希望其他人遇到了这一点。

您使用的是File方法的定义是:

FilePathResult File(string fileName, string contentType, string fileDownloadName) 

正如你所看到的,最后一个参数是文件下载的名称。所以你需要提供文件的名称,,包括扩展名

return File(FilePath, 
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", 
    "Specimen Template File.xlsx"); 
+0

哇,男人。认为这很明显,只是没有令人尴尬的明显。 谢谢你,这是我在本周起飞之前需要做的最后一件事情之一。有一个伟大的感恩节。 – TrevorGoodchild