Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Return image from .aspx link

Tags:

asp.net

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.

like image 253
George Johnston Avatar asked Feb 12 '10 16:02

George Johnston


2 Answers

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); 
  } 
}
like image 172
ziya Avatar answered Sep 23 '22 15:09

ziya


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();
like image 20
sashaeve Avatar answered Sep 21 '22 15:09

sashaeve