-1

I have this code for downloading images but when I click the download button it will start the download process but the download doesn't complete and no file is received but a type error is found.

<html>
<head>
<script type="text/javascript">
function prepHref(linkElement) {
    var myDiv = document.getElementById('fullsized_image_holder');
    var myImage = myDiv.children[0];
    linkElement.href = myImage.src;
}
</script>
</head>
<body>

<div id="fullsized_image_holder">

</div>
<a href="#" onclick="prepHref(this)" download>Click here to download image</a>
</body>
</html>
ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26

2 Answers2

0

You can use Jquery File Download Plugin ,refenced by Paolo Bergantino's answer

Example

prepHref(image){
    $.fileDownload('https://1.bp.blogspot.com/xDskNs1CNhU/W_uw44XJ70I/AAAAAAAAGTg/3trA_Gh3GsojvuhQ1eljASYla7Y5SulGQCLcBGAs/s200/Fadex.jpg')
       .done(function () { alert('File download a success!'); })
       .fail(function () { alert('File download failed!'); });
}
ahmeticat
  • 1,899
  • 1
  • 13
  • 28
0

You can use the below one.

<div id="div-id"><img src="yourImage.jpg" style="margin-top: 0px;"></div>

<a href="#" onclick="this.href = $('#div-id img:first').attr('src');" download>Clickdownload image</a>

div-id is the div which is holding the image.

Ravi
  • 36
  • 4