.NET 图像处理程序在下载时剥离文件类型

发布于 2024-09-26 05:49:28 字数 971 浏览 5 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-10-03 05:49:28

我认为您需要 image/jpeg 而不是 text/jpeg 作为 context.Response.ContentType 标头。您还需要设置有效的内容处置;你可能想要附件。当前您只有文件名参数,如果不指定内容处置类型,标头无效。你的标题最终应该是这样的:

Content-Disposition: attachment;filename=the_filename_here.ext

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文