FileContentResult returned as jpg but IE “Save pic

2019-08-05 14:54发布

问题:

I have an MVC 4 controller method returning a FileContentResult as follows:

[OutputCache(CacheProfile = "ImageCache")]
public FileContentResult GetImage(string dl)
{
    return File(photoSig.BinaryPhoto, "image/jpeg", "photo.jpg");
}

The ImageCache is defined as follows:
<caching> <outputCacheSettings> <outputCacheProfiles> <add name="ImageCache" duration="1" varyByParam="*" location="Server" enabled="true" /> </outputCacheProfiles> </outputCacheSettings> </caching>

Everything works fine except in IE, when the user does a Save picture as..., it only gives them the option to save as PNG or BMP. Why wouldn't it by a jpg? Chrome and FF give the jpg option as it should.

Another interesting point is that it works fine on my local box but not when deployed...which makes me think it could be a configuration issue. But I don't know where to start looking. The IIS MIME type for jpg is defined on the UI server.

回答1:

I believe the problem has something to do with the no-store cache-control header. See this link for more information: http://social.technet.microsoft.com/Forums/windows/en-US/e1f5c13b-92f0-4aa7-83b5-344edbe4093d/save-picture-as-jpeg-problem-ie7win-xp-pro-sp3?forum=itproxpsp

We ended up adding an IIS rewrite rule to solve the problem...so when it says no-store for a jpg file, we change it to something like public or private instead. Then I can save as jpg.