locked
Save external image using IHttpHandler RRS feed

  • Question

  • User1982656839 posted

     Hello, I cannot figure this out for the life of me. I am very new to asp.net and have no idea what im doing. hopefully someone can help me. I have an ecommerce site and I am currently pulling my images from amazon. I would like to have the images on my local machine so I dont have to link to amazon, but have over 5000 products and cannot do it manually.

    I have looked at this thread http://forums.asp.net/t/1128323.aspx and its pretty much exactly what I need but I am using asp.net and C# and i think this code is in vb. Im not sure how to convert it to c# either.

    I just need the images to dynamically save to my local computer.. Please help! 

    Saturday, May 2, 2009 1:06 PM

Answers

  • User854688209 posted

    Try it now:

    public class ImageReplay: IHttpHandler

    {

     

    public bool IsReusable { get { return true; }

    }

     

    public void ProcessRequest(System.Web.HttpContext context)

    {

     

    System.Net.
    HttpWebRequest extRequest = null;

    System.Net.HttpWebResponse extResponse = null;

    System.IO.Stream extResponseStream = null;

    byte[] imgBuffer = new byte[257];

    string imageUrl = null;

     

    try {

    //external image we want - needs error checking...

    imageUrl = context.Request.QueryString["imageurl"];

    //create a webrequest for it

    extRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(imageUrl);

    //get the external response

    extResponse = (System.Net.HttpWebResponse)extRequest.GetResponse();

    //get the external responses stream

    extResponseStream = extResponse.GetResponseStream();

     

    //clear our buffer and set up our response

    context.Response.Clear();

    context.Response.Buffer =
    false;

    //set our mime type to match the external contents mime type

    context.Response.ContentType = extResponse.ContentEncoding;

     

    //now read for the external stream 256 at a time while writing all read bytes onto our response stream

    int bytesRead = extResponseStream.Read(imgBuffer, 0, 256); while (bytesRead > 0) {

    context.Response.OutputStream.Write(imgBuffer, 0, bytesRead);

    bytesRead = extResponseStream.Read(imgBuffer, 0, 256);

     

    }

    }

    finally {

    //clean up

     

    if ((extResponse != null)) extResponse.Close();

     

    }

    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2009 3:14 PM
  • User1982656839 posted

     None of my images are showing up now. Heres the code I used....

      

    using System.Net;
    using System.IO;
    using System.Web.UI;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    
     //set the namespace to match your web
     namespace Aspdotnetstorefront
     {
     public class ImageReplay: IHttpHandler
    
    {
    
     
    public bool IsReusable { get { return true; }
    
    }
    
     
    public void ProcessRequest(System.Web.HttpContext context)
    
    {
    
     
    System.Net.HttpWebRequest extRequest = null;
    
    System.Net.HttpWebResponse extResponse = null;
    System.IO.Stream extResponseStream = null;
    
    byte[] imgBuffer = new byte[257];
    string imageUrl = null;
    
     
    try {
    
    //external image we want - needs error checking...
    
    imageUrl = context.Request.QueryString["imageurl"];
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
    Byte[] imgdata = new byte[response.ContentLength];
    Stream receiveStream = response.GetResponseStream();
    receiveStream.Read(imgdata,0,imgdata.Length);
    FileStream fs = new FileStream("c:\\yourtpath\\filename.jpg",FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(imgdata , 0,imgdata.Length);
    fs.Close();
    
     
    
    }
    finally {
    
    
     
    if ((extResponse != null)) extResponse.Close();
    
     
    
    }
    
    }
    
    } 
     }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2009 4:39 PM
  • User1677448765 posted

     go through this

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=214

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 3, 2009 2:46 AM

All replies

  • User854688209 posted

    Here is the C# code;

    1. namespace MySampleWeb
    2. {
    3.    
    4.     public class ImageReplay : IHttpHandler
    5.     {
    6.        
    7.         public bool IsReusable {
    8.             get { return true; }
    9.         }
    10.        
    11.         public void ProcessRequest(System.Web.HttpContext context)
    12.         {
    13.            
    14.             System.Net.HttpWebRequest extRequest = null;
    15.             System.Net.HttpWebResponse extResponse = null;
    16.             System.IO.Stream extResponseStream = null;
    17.             byte[] imgBuffer = new byte[257];
    18.             string imageUrl = null;
    19.            
    20.             try {
    21.                 //external image we want - needs error checking...
    22.                 imageUrl = context.Request.QueryString("imageurl");
    23.                 //create a webrequest for it
    24.                 extRequest = System.Net.WebRequest.Create(imageUrl);
    25.                 //get the external response
    26.                 extResponse = extRequest.GetResponse;
    27.                 //get the external responses stream
    28.                 extResponseStream = extResponse.GetResponseStream;
    29.                
    30.                 //clear our buffer and set up our response
    31.                 context.Response.Clear();
    32.                 context.Response.Buffer = false;
    33.                 //set our mime type to match the external contents mime type
    34.                 context.Response.ContentType = extResponse.ContentEncoding;
    35.                
    36.                 //now read for the external stream 256 at a time while writing all read bytes onto our response stream
    37.                 int bytesRead = extResponseStream.Read(imgBuffer, 0, 256);
    38.                 while (bytesRead > 0) {
    39.                     context.Response.OutputStream.Write(imgBuffer, 0, bytesRead);
    40.                     bytesRead = extResponseStream.Read(imgBuffer, 0, 256);
    41.                    
    42.                 }
    43.             }
    44.             finally {
    45.                 //clean up
    46.                    
    47.                 if ((extResponse != null)) extResponse.Close();
    48.                
    49.             }
    50.         }
    51.     }
    52. }

    I have used below url to convert vb.net to c#, refer below url whenever you need to convert vb.net to c#.

    http://www.developerfusion.com/tools/convert/vb-to-csharp/

    Saturday, May 2, 2009 2:35 PM
  • User1982656839 posted

     Hello, I have already tried this. It gives me errors....

     

    If you test it, you will see. 

    Saturday, May 2, 2009 2:44 PM
  • User854688209 posted

    Try it now:

    public class ImageReplay: IHttpHandler

    {

     

    public bool IsReusable { get { return true; }

    }

     

    public void ProcessRequest(System.Web.HttpContext context)

    {

     

    System.Net.
    HttpWebRequest extRequest = null;

    System.Net.HttpWebResponse extResponse = null;

    System.IO.Stream extResponseStream = null;

    byte[] imgBuffer = new byte[257];

    string imageUrl = null;

     

    try {

    //external image we want - needs error checking...

    imageUrl = context.Request.QueryString["imageurl"];

    //create a webrequest for it

    extRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(imageUrl);

    //get the external response

    extResponse = (System.Net.HttpWebResponse)extRequest.GetResponse();

    //get the external responses stream

    extResponseStream = extResponse.GetResponseStream();

     

    //clear our buffer and set up our response

    context.Response.Clear();

    context.Response.Buffer =
    false;

    //set our mime type to match the external contents mime type

    context.Response.ContentType = extResponse.ContentEncoding;

     

    //now read for the external stream 256 at a time while writing all read bytes onto our response stream

    int bytesRead = extResponseStream.Read(imgBuffer, 0, 256); while (bytesRead > 0) {

    context.Response.OutputStream.Write(imgBuffer, 0, bytesRead);

    bytesRead = extResponseStream.Read(imgBuffer, 0, 256);

     

    }

    }

    finally {

    //clean up

     

    if ((extResponse != null)) extResponse.Close();

     

    }

    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2009 3:14 PM
  • User1982656839 posted

     It appears to work without errors but I still have one question. Where are the images being saved and what name are they being saved as?

    Saturday, May 2, 2009 3:37 PM
  • User854688209 posted

    It will be saved in buffer, see below lines in the code:

    //now read for the external stream 256 at a time while writing all read bytes onto our response stream

    int bytesRead = extResponseStream.Read(imgBuffer, 0, 256); while (bytesRead > 0) {

    context.Response.OutputStream.Write(imgBuffer, 0, bytesRead);

    bytesRead = extResponseStream.Read(imgBuffer, 0, 256);

    Saturday, May 2, 2009 3:43 PM
  • User1982656839 posted

     thanks for your reply. I am not sure what a buffer is.. sorry in new to asp.net. Is there a way to save it to my web folder or my local machine? thanks

    Saturday, May 2, 2009 3:51 PM
  • User854688209 posted

    Try this:

    string imageUrl = null;
    imageUrl = context.Request.QueryString["imageurl"];
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
    Byte[] imgdata = new byte[response.ContentLength];
    Stream receiveStream = response.GetResponseStream();
    receiveStream.Read(imgdata,0,imgdata.Length);
    FileStream fs = new FileStream("c:\\yourtpath\\filename.jpg",FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(imgdata , 0,imgdata.Length);
    fs.Close();

    Saturday, May 2, 2009 4:16 PM
  • User1982656839 posted

     None of my images are showing up now. Heres the code I used....

      

    using System.Net;
    using System.IO;
    using System.Web.UI;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    
     //set the namespace to match your web
     namespace Aspdotnetstorefront
     {
     public class ImageReplay: IHttpHandler
    
    {
    
     
    public bool IsReusable { get { return true; }
    
    }
    
     
    public void ProcessRequest(System.Web.HttpContext context)
    
    {
    
     
    System.Net.HttpWebRequest extRequest = null;
    
    System.Net.HttpWebResponse extResponse = null;
    System.IO.Stream extResponseStream = null;
    
    byte[] imgBuffer = new byte[257];
    string imageUrl = null;
    
     
    try {
    
    //external image we want - needs error checking...
    
    imageUrl = context.Request.QueryString["imageurl"];
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
    Byte[] imgdata = new byte[response.ContentLength];
    Stream receiveStream = response.GetResponseStream();
    receiveStream.Read(imgdata,0,imgdata.Length);
    FileStream fs = new FileStream("c:\\yourtpath\\filename.jpg",FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(imgdata , 0,imgdata.Length);
    fs.Close();
    
     
    
    }
    finally {
    
    
     
    if ((extResponse != null)) extResponse.Close();
    
     
    
    }
    
    }
    
    } 
     }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2009 4:39 PM
  • User854688209 posted

    Are you able to save the images? if yes, you need to give the path of the saved location to the images.

    Saturday, May 2, 2009 4:55 PM
  • User1982656839 posted

     yes, I entered the correct file path and it saves the image. I am confused though because the external image isnt show in my page, but it still saves it....?!

    Saturday, May 2, 2009 5:31 PM
  • User854688209 posted

    most probaly image src path is not correct, can you please post the code where you are using images to show on the page.

    Sunday, May 3, 2009 1:28 AM
  • User1677448765 posted

     go through this

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=214

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 3, 2009 2:46 AM