Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Web Page Button with Save As option

Posted on 2013-11-27
10
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

715 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