开发者

Ashx force download image

开发者 https://www.devze.com 2023-02-25 05:59 出处:网络
I have an ashx file that display an image. I would like not to display the image but force the download.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消