Prompt "SaveAs" Dialog on Image click like for applications, .zip etc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Coldfire
    Contributor
    • Nov 2006
    • 289

    Prompt "SaveAs" Dialog on Image click like for applications, .zip etc

    Hello,

    The issue is: I have a webpage that displays image in img tag while taking its src from a remote url ( dynamically). Now, my client want this functionality to ask for "Save at any location" routine when someone clicks on the image so that user can avoid right-click saveas.

    Platform: xp/asp.net2.0/web-application

    PLz help asap
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well if you have an onclick handler for the image (which is tricky because the aspx page validator will always give you a warning saying "this tag does not support that feature" or something)

    if you want the picture to prompt for download you will probably have to do some middleman work. Your code will have to make a request for the picture on the remote server THEN send it back to your client (in response to their click) with an extra header that declares it to be downloaded.

    Something like:
    Code:
    string DefaultFileName="Whatever.jpg";//or whatever
    string myContentType="image/jpg"; //or whatever
    Response.Clear();
    Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    Response.ContentType = myContentType;
    Response.AddHeader("content-disposition", "attachment; filename=" + DefaultFileName);
    Response.Write(cbuff, 0, cbuff.Length);
    //where cbuff is a char[] containing your image data
    Response.End();

    Comment

    • Coldfire
      Contributor
      • Nov 2006
      • 289

      #3
      thanks that helped...but I am still struggling!

      Originally posted by Plater
      Well if you have an onclick handler for the image (which is tricky because the aspx page validator will always give you a warning saying "this tag does not support that feature" or something)

      if you want the picture to prompt for download you will probably have to do some middleman work. Your code will have to make a request for the picture on the remote server THEN send it back to your client (in response to their click) with an extra header that declares it to be downloaded.

      Something like:
      Code:
      string DefaultFileName="Whatever.jpg";//or whatever
      string myContentType="image/jpg"; //or whatever
      Response.Clear();
      Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
      Response.ContentType = myContentType;
      Response.AddHeader("content-disposition", "attachment; filename=" + DefaultFileName);
      Response.Write(cbuff, 0, cbuff.Length);
      //where cbuff is a char[] containing your image data
      Response.End();

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well break it down into steps, first you need to get the image clickable AND have the click be caught by your server code.

        I would suggest making a new aspx page called like ImageDownloader .aspx
        And make that page have its sole job is to perform those sneaky manuvers.

        So for an image like this:
        [html]
        <img id="myimage" src="http://www.google.com/intl/en_ALL/images/logo.gif">[/html]
        You could have an onclick method that goes to:
        Code:
        http://mywebsite/ImageDownloader.aspx?url=http://www.google.com/intl/en_ALL/images/logo.gif
        Then inside your ImageDownloader .aspx file, parse out the URL
        Code:
        if (Request.Params["url"]!=null)
        {
           // validate the string, attempt to fetch the data, then send it back with the code i posted earlier
        }
        else
        {
           // respond back with an error of somesort
        }

        Comment

        Working...