[英]how to pass a folder path to mvc controller
我正在尝试使用@Html.ActionLink
将文件夹路径传递给下载控制器,但是却找不到类似位置的错误
找不到文件'C:\\ Terth Content \\ Project \\ Colege \\ WebApp \\ Media \\ @ item.Content'
但是,当我给出硬编码值时,它确实起作用。 请问我有什么问题。
这是我的代码:操作方法:
public FileResult Download(string fileName, string filePath)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
string documentName = fileName;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, documentName);
}
视图
@Html.ActionLink("Download", "Download", "Marketing", routeValues: new
{
fileName = @item.Content,
filePath = Server.MapPath("~/Media/@item.Content"),
area = "AffiliateAdmin"
}, htmlAttributes: null)
就像评论中提到的一样,您的视图中有一个错误:
代码("~/Media/@item.Content")
呈现为C:\\Teerth Content\\Project\\Colege\\WebApp\\Media\\@item.Content
,您实际上需要在其中Server.MapPath("~/Media/" + @item.Content)
查找实际的文件名。
但是您需要重新考虑这种设计,因为它会将您的整个计算机开放到网络上。 一定有人会尝试Download("C:\\Teerth Content\\Project\\Colege\\WebApp\\web.config", "web.config")
,公开您的连接字符串和其他应用程序设置,更不用说您服务器上的其他文件了确实不希望客户下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.