Bob
Bob

Reputation: 1061

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.AppendHeader("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

Upvotes: 1

Views: 1425

Answers (2)

pstarkov
pstarkov

Reputation: 149

Your browser always will show pictures in window if they contentType wiil be "image/jpeg". Try to use different contentType.

Upvotes: 0

Jakob Gade
Jakob Gade

Reputation: 12419

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...

Upvotes: 1

Related Questions