开发者

FileResult returning corrupt file

I'm creating a simple controller action in ASP.NET MVC 2 (under the .NET 4.0 framework) which will resize files.

I've got a controller like this (I've cut it down a bit):

public ActionResult GetFile(int fileId, string fileSource) {
    FileInfo file = repo.FindFileById(fileId);

    //do some resizing

    string mimeType = string.Empty;
    switch(file.Extension) {
        case ".jpg":
            mimeType = "image/jpg";
            break;
        //some more stuff
        default:
            mimeType = "text/png";
            break;
    }

    return File(file.FullName, mimeType);
}

On the file system it saves fine, I can view the resized file, but in the browser the file doesn't render.

I've used Charles to inspect the response and it comes back with a HTTP Status of 200, but the Image isn't visible.

If I try and save the image and view it Windows picture viewer says that it's corrupt.

I've also tried wit开发者_高级运维h ZIP (sans resizing ;)) and it returns a corrupt ZIP file.

I'm sure I'm just doing something wrong but I can't for the life of me spot it.

Edit

I've tested in both Cassini and IIS 7.5 (Windows 7) and recieve the problem in both instances.


I would recommend you to proceed by elimination. Start with a simple action and a working jpeg image:

public ActionResult Index()
{
    return File("test.jpg", "image/jpeg");
}

If this doesn't work then you might be having some other problem (you might also try clearing the browser cache). If it works replace the test.jpg by the one you have but before resizing, etc, until you find the problem.


The correct mime type for a jpeg is image/jpeg, not image/jpg. This will cause issues.

You should also post your resizing code as this is likely the cause of your corruption. What happens if you just read the fine and write it back out?


It turns out the problem is caused by a Http Compression module which is running on the site, it's trying to transform the image response, corrupting the image.


Try using FileResult instead of ActionResult

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜