Ashx force download image
I have an ashx file that display an image. I would like not to display the image but force the download.
This is my code :
context.Response.Append开发者_开发技巧Header("content-disposition", "attachment; filename=" + userId + ".jpg");
context.Response.ContentType = "image/jpeg";
context.Response.OutputStream.Write(msMasterFinal.ToArray(), 0, msMasterFinal.ToArray().Length);
When I open my browser with this ashx, the image is automatically displayed. How to force download please ?
Many thanks
Hmm, you code looks right. I've used this snippet in the past to achieve what you're trying to do:
context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=""{0}""", FileName));
context.Response.AddHeader("Content-Type", FileType);
context.Response.AddHeader("Content-Length", FileSize.ToString);
context.Response.BinaryWrite(FileBytes);
It's almost similar...
Your browser always will show pictures in window if they contentType wiil be "image/jpeg". Try to use different contentType.
精彩评论