.NET 图像处理程序在下载时剥离文件类型
我创建了一个 ashx 处理程序来渲染 mysql 数据库中图像的图像缩略图。如果通过查询字符串传递文件名,则会设置内容处置文件名(当用户单击“另存为...”时,将显示文件名)。当用户选择“另存为...”时,图像正确显示并且文件名出现,但文件类型被列为未知并且下载的文件没有类型。
我尝试在内容配置中将“.jpg”添加到文件名末尾,因为没有其他可尝试的内容,但这使得每个图像下载为 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 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 download 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;");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要 image/jpeg 而不是 text/jpeg 作为
context.Response.ContentType
标头。您还需要设置有效的内容处置;你可能想要附件。当前您只有文件名参数,如果不指定内容处置类型,标头无效。你的标题最终应该是这样的: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: