views:

19

answers:

1

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;");
        }
+1  A: 

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 content disposition 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
Mark
yup, your content type is wrong. That should fix that issue
jimplode
Content type was the issue, thanks so much! I went ahead and updated my content-disposition header too as you suggested.
caltrop