【发布时间】: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