Reputation: 3155
We have an action for image file download. the code is
public virtual FileResult Attachment(Guid Id)
{
...code for attachment..
Response.AddHeader("content-disposition", "attachment; filename=" + attachment.FileName);
Response.AddHeader("content-type", attachment.ContentType);
Response.AddHeader("cache-control", "must-revalidate");
return File(attachment.Content, attachment.ContentType);
}
When we test this action, we always get corrupt file. I compared the corrupt file with the orginal user notepad++ and found there is one new line character added to the file. I deleted the new line in notepad++ and the image was fixed.
We checked the content of the attachment and we are sure the content was correct.
We are running .net mvc 3. the content type we had for png is "image/png"
Upvotes: 1
Views: 989
Reputation: 30152
Fiddler the result (ie fiddler2.com) and look at the headers and the result. You can save the results from there as well and test the file. Is the newline at the beginning or the end? if the beginning, its likely headers. IF its at the end, are you running any ActionFilters that could be adding an empty result (or so you think) that actually is an added new line? Test it with another file - same results?
Test it on another system if possible - same results?
Upvotes: 1
Reputation: 10774
Check what other headers have already populated in the response prior to (or after) adding your custom headers. It may be that you are getting some extraneous headers that are causing the problem.
It may be as simple as calling ClearHeaders
A tool such as the Live Http Headers firefox addon may be helpful as well.
Upvotes: 1