2015-03-08 52 views
0

C#的Web MVC +角如何防止URL编码传递参数时,方法在MVC控制器

我有我的网页上的链接如下:

<a ng-show="channel.ArtistImagepath != null" ng-href="/channel/DownloadImageFile/?filepath={{channel.ArtistImagepath}}" target="_blank"><i class="fa fa-download" style="color: green"></i> Artist Image</a><br/> 

当我复制链接我浏览器是:

//mysite/channel/DownloadImageFile/?filepath=ChannelUploads/b51535d5-4ea3-45ae-bda4-332a0ce19ee8Artist+Gomez.jpg 

当我点击的文件路径发送到我的控制器的链接是: ChannelUploads/b51535d5-4ea3-45ae-bda4-332a0ce19ee8ArtistDina Gomez.jpg

正如您所看到的,由于URL已被编码,所以+号被替换为文件名中的空格。这将导致错误未找到现有的我的服务器上message..the实际文件名的文件是:b51535d5-4ea3-45ae-bda4-332a0ce19ee8Artist + Gomez.jpg

public ActionResult DownloadImageFile(string filepath) 
{ 
    filepath = "https://marvment.com/" + filepath; 

    var type = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); 
    var path = Path.GetFileName(filepath); 

    path = HttpUtility.UrlEncode(path); 
    //path = ToUrlFriendlyString(path); 

    using (var client = new WebClient()) 
    { 
     var buffer = client.DownloadData(filepath); 
     return File(buffer, type.ToString(), path); 
    } 
} 

如何防止文件名从beign网址编码之前发送到我的控制器? 我想在文件名中保留+号,并在我的MVC控制器中处理编码。

回答

1

How can I prevent the file name from beign URL encoded before sending it to my controller?

其实它是你需要做的完全相反:确保您的网址参数(文件名)被赋予浏览器首先之前URL编码。在这种情况下,这应该在Angular中完成;请参阅How to generate url encoded anchor links with AngularJS?

您不能在控制器动作中正确解决这个问题,因为在动作获取文件名时,它会自动为您解码。

+0

我试过了,不断收到错误:https://docs.angularjs.org/error/$injector/unpr?p0 = escapeFilterProvider%20%3C-%20escapeFilter – user1526912 2015-03-08 23:02:43

相关问题