Solved

Web Page Button with Save As option

Posted on 2013-11-27
10
308 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

772 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