1

C# Web MVC + Angular

I have the below link on my web page:

<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/>

When I copy the link in my browser it is:

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

When I click on the link the filepath sent to my controller is: ChannelUploads/b51535d5-4ea3-45ae-bda4-332a0ce19ee8ArtistDina Gomez.jpg

As you can see the + sign is replaced with a space in the filename because the URL is encoded. This results in a file not found error message..the actual filename existing on my server is: 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);
    }
}

How can I prevent the file name from beign URL encoded before sending it to my controller? I want to keep the + sign in the filename and handle the encoding in my MVC controller.

user1526912
  • 15,818
  • 14
  • 57
  • 92

1 Answers1

1

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

Actually it's the exact opposite that you need to do: make sure your URL parameter (the filename) is URL-encoded before being given to the browser in the first place. In this instance this should be done in Angular; see How to generate url encoded anchor links with AngularJS?

You cannot correctly address this inside your controller action, because by the time the action gets the filename it has been automatically decoded for you.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • I tried it and keep getting an error:https://docs.angularjs.org/error/$injector/unpr?p0=escapeFilterProvider%20%3C-%20escapeFilter – user1526912 Mar 08 '15 at 23:02