Download the image from an external domain (AWS S3) and save it to your browser

I am new to ASP.net and wondering how easy it is to download a photo from an external domain (Amazon S3) using their expiring link and save the photo in browser memory for another script to pick which uses the OpenBinary method? This allows me to resize and watermark before printing on the screen.

This is what I want:

In loadImage.aspx, I get a photo ID from my database, create a signed URL for Amazon S3, call the photo somehow and save it in memory. When in memory, my ASP.Jpeg script will call the OpenBinary method, resize and watermark the photo and use the SendBinary method to display the photo.

I think a memory stream or a binary response record may be the thing I'm looking for, but don't know how to use it on an external source. This is what I still managed, but was embarrassed and thought I was getting help, because I'm not sure if this will work if I can load a photo of external domains into memory, if I miss something important ...

My picture element:

<img src="loadImage.aspx?p=234dfsdfw5234234"> 

On loadImage.aspx:

 string AWS_filePath = "http://amazon............" using (FileStream fileStream = File.OpenRead(AWS_filePath)) { MemoryStream memStream = new MemoryStream(); memStream.SetLength(fileStream.Length); fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length); } // Persits ASP.Jpeg Component objJpeg.OpenBinary( ... ); // resize bits // watermark bits objJpeg.SendBinary( ... ); 

Any help would be awesome.

+4
source share
1 answer

First, start using the .ashx handler, not the full .aspx page. The handler didn’t have all the aspx page calls, it’s more clear what you are going to send, and you avoid all existing existing headers.

 <img src="loadImage.ashx?p=234dfsdfw5234234"> 

How to upload an image.

 string url = "http://amazon............" byte[] imageData; using (WebClient client = new WebClient()) { imageData = client.DownloadData(url); } 

How to send image to browser

 // this is the start call from the handler public void ProcessRequest(HttpContext context) { // imageData is the byte we have read from previous context.Response.OutputStream.Write(imageData, 0, imageData.Length); } 

How to set cache and header

  public void ProcessRequest(HttpContext context) { // this is a header that you can get when you read the image context.Response.ContentType = "image/jpeg"; // the size of the image context.Response.AddHeader("Content-Length", imageData.Length.ToString()); // cache the image - 24h example context.Response.Cache.SetExpires(DateTime.Now.AddHours(24)); context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0)); // render direct context.Response.BufferOutput = false; ... } 

I hope these tips help you move on.

relative:
https://stackoverflow.com/search?q=%5Basp.net%5D+DownloadData

+5
source

All Articles