【问题标题】:Download file from Details View MVC 5从详细信息视图 MVC 5 下载文件
【发布时间】:2016-04-08 08:12:25
【问题描述】:

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

这是我的控制器

 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\'

有什么想法吗? 谢谢

【问题讨论】:

  • 在 ActionLink 中传递“id”参数,而您应该使用在下载操作中声明的“file”
  • 谢谢,我改成@Html.ActionLink("Download", "Download", "announcement", Model.file)。但它返回相同的错误。当使用 firebug 进行调试时,它会将链接呈现为 Download
  • 我认为 ancor 助手必须像 "@Html.ActionLink("Download", "Download", "announcement", new { file= Model.file})" ,应该呈现为"/announcement/Download?file=[yourModel.File]"
  • 如果添加断点,FileVirtualPath 显示为什么?
  • 现在它呈现 Download

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


【解决方案1】:

你需要使用下面的重载;

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

在末尾添加空参数使用如下:

LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, RouteValueDictionary, IDictionary)

Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多