16

Is it possible to output an image (or any file type) to a download link when a user clicks on a link from another ASP.NET page?

I have the file name and byte[].

<a href="getfile.aspx?id=1">Get File</a>

...where getfile returns the file instead of going to the getfile.aspx page.

George Johnston
  • 31,652
  • 27
  • 127
  • 172

7 Answers7

19

You would want .ashx for that really ;)

public class ImageHandler : IHttpHandler 
{ 
  public bool IsReusable { get { return true; } } 

  public void ProcessRequest(HttpContext ctx) 
  { 
    var myImage = GetImageSomeHow();
    ctx.Response.ContentType = "image/png"; 
    ctx.Response.OutputStream.Write(myImage); 
  } 
}
mtmk
  • 6,176
  • 27
  • 32
  • 1
    .aspx would go through the page life cycle. .ashx is better here because there is no need for that, so probably performs better. See also: http://stackoverflow.com/questions/5469491/aspx-vs-ashx-main-difference – mtmk Sep 08 '12 at 15:21
5

Here is how I have done this in the past:

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", string.Format("inline; filename=\"{0}.pdf\"",Guid.NewGuid()));
Response.ContentType = @"application/pdf";
Response.WriteFile(path);
mxmissile
  • 11,464
  • 3
  • 53
  • 79
5

How to Create Text Image on the fly with ASP.NET

Something like this:

string Path = Server.MapPath(Request.ApplicationPath + "\image.jpg");
Bitmap bmp = CreateThumbnail(Path,Size,Size);
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
sashaeve
  • 9,387
  • 10
  • 48
  • 61
  • ashx is most of the time what you want BUT sometime you really need a aspx page for instance if you using the MapPageRoute that doesn't accept an ashx. – Zyo Mar 09 '13 at 02:59
1

Yeah, you have to clear the response completely and replace it with the image byte data as a string, and you need to make sure to set the response header for content-type according to the type of image

Rich
  • 36,270
  • 31
  • 115
  • 154
  • What if I do not have the content type? – George Johnston Feb 12 '10 at 16:20
  • Infer it from the file extension. That's all the OS is doing anyway (change a jpg extension to a gif and ask any built-in function or utility what type of file it is). – Rich Feb 12 '10 at 16:22
  • 2
    According to http://www.w3.org/Protocols/rfc1341/4_Content-Type.html and http://en.wikipedia.org/wiki/Internet_media_type you should use "application/octet-stream" for an unknown content-type. But if your sending an image to be shown in the page then the browser will have to know the content-type and you have to do what @Rich says. But the octet-stream content type will work if your only what to save the file as the browser doesn't care about what it is. – Mariano Desanze Feb 12 '10 at 16:32
1

Yes, this is possible. There are two parts of the Response object you need to set: the Content-Type and the HTTP Header. The MSDN documentation has the details on the response object but the main concept is pretty simple. Just set the code to something like this (for a Word doc).

Response.ContentType="application/ms-word";
Response.AddHeader("content-disposition", "attachment; filename=download.doc");

There is a more complete example here

Peter Jacoby
  • 2,406
  • 25
  • 26
0

the codebehind code for getfile.aspx has to have a content-type and the browser will know that it is an image or a unknown file and will let you save it.

In asp.net you can set the ContentType by using the Response object, i.e.

Response.ContentType = "image/GIF"

Here you have a tutorial for dynamically generated image

Mariano Desanze
  • 7,847
  • 7
  • 46
  • 67
0

ashx...

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext ctx)
    {
       string path = ".....jpg";

                byte[] imgBytes = File.ReadAllBytes(path);
                if (imgBytes.Length > 0)
                {
                    ctx.Response.ContentType = "image/jpeg";
                    ctx.Response.BinaryWrite(imgBytes);
                }
    }

    public bool IsReusable
    {
        get {return false;}
    }
}
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
ina
  • 83
  • 1
  • 5