从详细信息视图MVC 5下载文件

时间:2016-04-08 08:12:26

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5

您好我尝试从服务器下载文件。

这是我的控制器

 public ActionResult Downloads()
    {
        var dir = new System.IO.DirectoryInfo(Server.MapPath("~/Content/AnnFiles/"));
        System.IO.FileInfo[] fileNames = dir.GetFiles("*.*"); List<string> items = new List<string>();
        foreach (var file in fileNames)
        {
            items.Add(file.Name);
        }
        return View(items);
    }

    public FileResult Download(string file)
    {

        var FileVirtualPath = "~/Content/AnnFiles/" + file;
        return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
    }  

在我看来

 @Html.ActionLink("Download", "Download", "announcement", new { id = Model.file})

它不起作用。它返回错误

Could not find a part of the path 'C:\Myprojects\MyprojectName\MyprojectName\Content\AnnFiles\'

有什么想法吗? 谢谢

1 个答案:

答案 0 :(得分:2)

您需要使用以下重载;

@Html.ActionLink("Download", "Download", "announcement", new { file = Model.file}, null);

在末尾添加null参数使用以下内容:

<强> LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, RouteValueDictionary, IDictionary)

<强> Fiddle