为什么文件没有扩展名下载?

时间:2017-09-12 12:19:59

标签: c# asp.net-mvc download

该文件存储在我的应用文件夹App_Data中,当我下载时,它会以“下载”名称下载,文件类型为File我想下载原始名称的文件,它的正确延伸。任何帮助?

public ActionResult DownloadsFile()
{
    var dir = new System.IO.DirectoryInfo(Server.MapPath("~/App_Data/Files"));
    System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");
    List<string> items = new List<string>();

    foreach (var file in fileNames)
    {
        items.Add(file.Name);
    }

    return View(items);
}


<div class="st-pusher well" style="overflow:scroll;" id="content">
<br><h3 style="margin-left:10px; text-align:center;">Download Files</h3>
<div style="border: 1px solid silver; margin:10px 10px 0px 10px; padding: 10px; border-radius: 4px; background-color:white;">
    <table style="width:100%;" class="table table-bordered">
        <tr>
            <th>File Name</th>
            <th>Link</th>
        </tr>
        @for (var i = 0; i <= Model.Count - 1; i++)
        {
            <tr>
                <td>
                    @Model[i].ToString()
                </td>
                <td>
                    @Html.ActionLink("Download", "Download", new { FileName = @Model[i].ToString() })
                </td>
            </tr>
        }
    </table>
</div>


public FileResult Download(string FileName)
{
    return new FilePathResult("~/App_Data/Files/" + FileName, System.Net.Mime.MediaTypeNames.Application.Octet);
}

1 个答案:

答案 0 :(得分:4)

因为您没有设置FilePathResult的{​​{3}}。

您可能想要做的是:

return new FilePathResult("~/App_Data/Files/" + FileName, System.Net.Mime.MediaTypeNames.Application.Octet)
{
    FileDownloadName = "SetTheFileName"
};

FileDownloadName属性将设置响应的content-disposition标头,并将由浏览器的Save File对话框使用