Solved

Web Page Button with Save As option

Posted on 2013-11-27
10
309 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:RobertEhinger
  • 4
  • 2
  • 2
10 Comments
 
LVL 2

Accepted Solution

by:
pinish earned 500 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:RobertEhinger
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 500 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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:RobertEhinger
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:RobertEhinger
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:RobertEhinger
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

765 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