Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  86] [ 7]  / answers: 1 / hits: 38066  / 11 Years ago, sun, november 3, 2013, 12:00:00

On my website I have a jQuery script that, if the download button is clicked it will open the image that you want in new window.



My question is, how can I make this script when you click the button the image will save automatically and not open in a new window.



My code :



<script type=text/javascript>
$(document).ready(function (){
$('#download-btn').click(function(){
var size = $('#size').val();
window.open(size);
});
})
</script>

More From » jquery

 Answers
26

First I try jqueryfiledonwloader but not work on image file,after a some searching I found below solution,This work for me charmly,try this



 <script type=text/javascript>
$(document).ready(function (){
$('#download-btn').click(function(){
var link = document.createElement('a');
link.href = '/sites/default/files/toy/jpeg/image-1419683919_4851.jpeg'; // use realtive url
link.download = 'MyToy.jpeg';
document.body.appendChild(link);
link.click();
});
})
</script>

[#74537] Friday, November 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;