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

Asp.net .NET映像处理程序在下载时剥离文件类型,asp.net,download,ashx,content-disposition,Asp.net,Download,Ashx,Content Disposition,我创建了一个ashx处理程序,用于从mysql数据库中的图像渲染图像缩略图。如果文件名通过querystring传递,则会设置内容处置文件名(当用户单击“另存为…”时,会显示文件名)。当用户选择“另存为…”时,图像会正确显示,文件名也会显示,但文件类型列为未知,下载的文件没有类型 我已经尝试过在content disposition中的文件名末尾添加“.jpg”,因为没有其他东西可以尝试,但这使得每次下载的图像都是untitle.bmp byte[] imageData = null; Im

我创建了一个ashx处理程序,用于从mysql数据库中的图像渲染图像缩略图。如果文件名通过querystring传递,则会设置内容处置文件名(当用户单击“另存为…”时,会显示文件名)。当用户选择“另存为…”时,图像会正确显示,文件名也会显示,但文件类型列为未知,下载的文件没有类型

我已经尝试过在content disposition中的文件名末尾添加“.jpg”,因为没有其他东西可以尝试,但这使得每次下载的图像都是untitle.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;");
        }

我想您的
context.Response.ContentType
标题需要的是image/jpeg而不是text/jpeg。您还需要设置一个有效的;你可能想要依恋。当前只有filename参数,如果不指定内容处置类型,则标题无效。您的标题最终应如下所示:

Content-Disposition: attachment;filename=the_filename_here.ext

内容类型是问题所在,非常感谢!我也按照你的建议更新了我的内容配置标题。