Currently I’m building a image gallery component for a website where the user is able to upload images and they are displayed in a gallery using http://pikachoose.com. I have one action on my controller called Download which takes the id of the image that should be downloaded as an argument. It worked fine using this code (in the beginning):
1: public ActionResult Download(int id)
2: {
3: var image = Repository.GetById(id);
4:
5: return File(image.Data, image.ContentType, image.Filename);
6: }
The problem with this code only showed up with some images. The problem is the last parameter to File(), image.Filename. Which is the filename of the original image the user has uploaded. If the filename contains “invalid characters” ASP.NET MVC throws this exception:
[FormatException: An invalid character was found in the mail header.]
System.Net.Mime.MailBnfHelper.GetTokenOrQuotedString(String data, StringBuilder builder)
System.Net.Mime.ContentDisposition.ToString() +270
System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +164
The part that confused me was “in the mail header”. Suppose they’re reusing some mail component for mime parsing. To fix it I only had to make sure that the filename didn’t had any invalid characters, I did it by setting it to the filename I use on the server instead of the filename submitted by the user.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5