Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download image on button click using jQuery

I have an image in my page. I want the image to be downloaded when I click on a button. How can I do this using jQuery or Javascript? Kindly provide me with a fiddle. FIDDLE

<div id="download">
    <img src="http://www.glamquotes.com/wp-content/uploads/2011/11/smile.jpg" id="image">
    <button id="dwnld"> Download image </button>
</div>
like image 573
Anusha Honey Avatar asked Sep 20 '13 19:09

Anusha Honey


People also ask

How to trigger a file download when clicking an HTML button?

To trigger a file download on a button click we will use a custom function or HTML 5 download attribute. The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded.

How can download PDF with button click in jQuery?

Downloading PDF File on Button Click using jQueryInside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the jQuery AJAX function. Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the PDF file is downloaded as Byte Array (Binary Data).


1 Answers

You can actually do this with the HTML5 download attribute, if older browsers are an issue, you should use the serverside for this, and set the appropriate headers etc.

<a href="http://www.glamquotes.com/wp-content/uploads/2011/11/smile.jpg" download="smile.jpg">Download image</a>

FIDDLE

like image 128
adeneo Avatar answered Sep 28 '22 00:09

adeneo