2015-04-17 75 views
0

我想传递一个文件夹路径使用@Html.ActionLink一个下载器,但我越来越找不到像如何通过一个文件夹路径MVC控制器

找不到文件“C的位置误差:\ Teerth 内容\项目\ Colege \ Web应用程序\媒体\ @ 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) 
+4

'filePath = Server.MapPath(“〜/ Media /”+ @ item.Content)' –

+0

谢谢你发现问题。 – Teerth

回答

2

就像提到in comments,你有你的观点的错误:

代码("~/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"),暴露您的连接字符串和其他应用程序设置,更不用说您的服务器上的其他文件,你真的不希望客户端下载。

相关问题