By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,321 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,321 IT Pros & Developers. It's quick & easy.

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

Coldfire
100+
P: 289
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
May 22 '07 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
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:
Expand|Select|Wrap|Line Numbers
  1. string DefaultFileName="Whatever.jpg";//or whatever
  2. string myContentType="image/jpg"; //or whatever
  3. Response.Clear();
  4. Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
  5. Response.ContentType = myContentType;
  6. Response.AddHeader("content-disposition", "attachment; filename=" + DefaultFileName);
  7. Response.Write(cbuff, 0, cbuff.Length);
  8. //where cbuff is a char[] containing your image data
  9. Response.End();
  10.  
May 22 '07 #2

Coldfire
100+
P: 289
thanks that helped...but I am still struggling!

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:
Expand|Select|Wrap|Line Numbers
  1. string DefaultFileName="Whatever.jpg";//or whatever
  2. string myContentType="image/jpg"; //or whatever
  3. Response.Clear();
  4. Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
  5. Response.ContentType = myContentType;
  6. Response.AddHeader("content-disposition", "attachment; filename=" + DefaultFileName);
  7. Response.Write(cbuff, 0, cbuff.Length);
  8. //where cbuff is a char[] containing your image data
  9. Response.End();
  10.  
May 22 '07 #3

Plater
Expert 5K+
P: 7,872
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:
Expand|Select|Wrap|Line Numbers
  1. http://mywebsite/ImageDownloader.aspx?url=http://www.google.com/intl/en_ALL/images/logo.gif
  2.  
Then inside your ImageDownloader.aspx file, parse out the URL
Expand|Select|Wrap|Line Numbers
  1. if (Request.Params["url"]!=null)
  2. {
  3.    // validate the string, attempt to fetch the data, then send it back with the code i posted earlier
  4. }
  5. else
  6. {
  7.    // respond back with an error of somesort
  8. }
  9.  
May 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.