c# - ASHX image handler works with chrome, not IE8 -
i've created code retrieve image file system using ashx handler. code displays image correctly in chrome, broken image in ie:
public class imagehandler : ihttphandler { private int? qs_imageid { { int? temp = null; if (httpcontext.current.request.querystring["imageid"] != null) temp = ha.utility.datahelper.parsedbint(httpcontext.current.request.querystring["imageid"]); return temp; } } private bool qs_thumbnail { { bool thumbnail = false; if (httpcontext.current.request.querystring["thumbnail"] != null) { if (httpcontext.current.request.querystring["thumbnail"].equals("1")) thumbnail = true; } return thumbnail; } } public void processrequest(httpcontext context) { if (qs_imageid.hasvalue) { int uploadid = qs_imageid.value; context.response.clearheaders(); context.response.clearcontent(); context.response.cache.setnostore(); context.response.contenttype = uploaddal.getmetadata(uploadid).uploadcontenttype; context.response.addheader("content-disposition", "inline;"); if (qs_thumbnail) { byte[] myimage = uploaddal.getfilethumbnail(uploadid); context.response.addheader("content-length", myimage.getlength(0).tostring()); context.response.binarywrite(myimage); context.response.end(); } else { byte[] myimage = uploaddal.getfile(uploadid); context.response.addheader("content-length", myimage.getlength(0).tostring()); context.response.binarywrite(myimage); context.response.end(); } } } public bool isreusable { { return false; } } }
the problem didn't specify mime type file. here common image mime types, function meant return corresponding mime type of file:
string getcontenttype(string path) { switch (path.getextension(path)) { case ".bmp": return "image/bmp"; case ".gif": return "image/gif"; case ".jpg": return "image/jpeg"; case ".png": return "image/png"; default : break; } return ""; }
if image physically stored on hard drive, can use piece of code is, if not @ least can give idea how determine mime type. in processrequest
method, use
context.response.contenttype = getcontenttype(imagefilename);
of course said before, if don't have physical path file (for example if it's stored in database) should still know image type.
hope helps,
andrei
Comments
Post a Comment