There are pictures, after clicking on the picture, the code is executed:

var isAdmin = confirm("Скачать"); 

How to make sure that when true returns true download of the image that was clicked begins?

    2 answers 2

    To allow the user to upload an image or other file you can use the HTML5 download attribute.

    https://stackoverflow.com/questions/7951326/save-image-to-users-disk-using-javascript/37521282#37521282

    You can emulate this download.

     function saveUrlAsFile(url, fileName) { var link = document.createElement("a"); link.setAttribute("href", url); link.setAttribute("download", fileName); link.click(); } $('img').on('click',function(e){ var isAdmin = confirm("Скачать?"); if(isAdmin){ saveUrlAsFile($(e.target).attr('src'), 'image.jpg'); } }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <img width="100" src="https://pbs.twimg.com/profile_images/378800000567859274/3d56eadbc1e389780a95c74189ebfa64.jpeg" /> <img width="100" src="https://shkolazhizni.ru/img/content/i133/133607_or.jpg" /> 

    IE not supported

    • Just top) Century live and learn: D - SkyFox

     $('img').on('click',function(e){ var isAdmin = confirm("Скачать"); if(isAdmin){ document.location.href = $(e.target).attr('src'); } }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <img width="100" src="https://pbs.twimg.com/profile_images/378800000567859274/3d56eadbc1e389780a95c74189ebfa64.jpeg" /> <img width="100" src="https://shkolazhizni.ru/img/content/i133/133607_or.jpg" /> 

    And then the magic from the server side (For example, PHP):

     header('Content-Disposition: attachment; filename="123.jpg"'); 
    • The problem is that the links of all the pictures are different - Gnevich
    • Download does not start, just opens the picture - Gnevich
    • In order for it to be downloaded (and not opened) it is necessary that the server indicate in the header: Content-Disposition: attachment; This can be done both in the back-end and in the web server settings. - Maxim