Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Web Page Button with Save As option

Posted on 2013-11-27
10
Medium Priority
?
320 Views
Last Modified: 2013-12-27
This is the web page where I have the sample code I am working with. I want to use javascript to create a button that will allow the user to download the image they see on the screen. So far all I have been able to get is the image in a new page with no header. What I want is a "Save As" pop up option. How can I accomplish this.

http://www.mrtechnology1954.com/santaclaus/santa24.html

Thank you!

Robert
0
Comment
Question by:Robert Ehinger
  • 4
  • 2
  • 2
10 Comments
 
LVL 2

Accepted Solution

by:
pinish earned 2000 total points
ID: 39681868
Try:-

<script>
function SaveFile(fname){
img.document.execCommand('saveas', null ,fname)
}
</script>

<iframe id="img" src="yourimage.jpg" width="(image width + 20)px" height="(image height + 25)px" scrolling="no" frameborder="0px"></iframe>


<button onclick="SaveFile('yourimage.jpg');">save as</button>
0
 

Author Comment

by:Robert Ehinger
ID: 39681979
That works except there is one small problem. I now have a black rectangle above the image and next to the save as button. What did I do wrong to cause that?
0
 
LVL 2

Assisted Solution

by:pinish
pinish earned 2000 total points
ID: 39682006
To be completely fair it is a bit of a hack and probably doesn't work in some  browsers.

Better way to do this is server side - do you have PHP or something like that available to you?  The "correct" way is to force the content type which will prompt the client to download rather than display the image - there are loads of examples to do it that way.  If you still really want a javascript button you can set the action to be the server side script which will do the same thing.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:jb1dev
ID: 39682034
The best way  to do this really is on the server.

Some other options are the HTML5 download attribute, that is for link, not button:
http://davidwalsh.name/download-attribute
Though that doesn't work on my browser. (Firefox, Ubuntu)

You could also base64 encode the image. Though your image is huge. See example:
http://jsfiddle.net/5whKM/

I've seen people claim they can extract the the img src and change the type to data:application/octet-stream forcing the browser to open a Save As dialog, but again I cannot recreate this in my env.
0
 

Author Comment

by:Robert Ehinger
ID: 39682089
I have Dreamweaver CS4 which is what I used to build all the pages associated with this site. I can easily add a button to the page but I don't know where to go to get the appropriate actions that I want associated with the button.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682144
I think what pinish and I are both saying is that the proper way to do this is to use a server side script to set the content type and serve the image.

Do you have php available on your server?

Any javascript actions you can associate with the button are client side scripts only and will not actually do what you are looking for.
0
 

Author Comment

by:Robert Ehinger
ID: 39686382
the server where this will ultimately reside does not have php available. Are there any other options? If I have to use the javascript how can I control the size of the image and is there a way to have it centered on the screen when it is opened?
0
 

Author Comment

by:Robert Ehinger
ID: 39738307
The owner of the web site decided he just needed for the visitor to be able to print the image with no header. That has been accomplished so the problem is resolved.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question