.NET image handler stripping filetype on download
I have created an ashx handler to render image thumbnails from images in a mysql database. If a file name is passed through querystring, the content disposition filename is set (when user clicks "save as..." the filename appears). The images appear properly and the filename appears when user selects "save as..." but the filetype is listed as unknown and the file that downloads has no type.
I have tried adding ".jpg" to the end of the filename in content disposition for lack of anything else to try, but this made every image downl开发者_JAVA百科oad as untitled.bmp.
byte[] imageData = null;
Image outputImage = null;
if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["pictureid"]))
pictureId = SafeConvert.ToInt(HttpContext.Current.Request.QueryString["pictureid"].Trim());
if (pictureId > -1)
{
if (!String.IsNullOrEmpty(fileName))
HttpContext.Current.Response.AppendHeader("Content-Disposition", "filename=" + fileName + ";");
imageData = new OHTManager().GetOrnamentImage(pictureId);
context.Response.ContentType = "text/jpeg";
context.Response.BinaryWrite(imageData);
}
else
{
throw new Exception("No image could be produced;");
}
I think you want image/jpeg instead of text/jpeg for your context.Response.ContentType
header. You also need to set a valid content disposition; you probably want attachment. Currently you have only the filename parameter, and the header is NOT valid without specifying the content disposition type. Your header should ultimately look like this:
Content-Disposition: attachment;filename=the_filename_here.ext
精彩评论