Force image download jQuery -


i have been working code below force images download. yes i'm aware of html5 attribute not work browsers. problem seems execute actual button calls function

    function savetodisk(fileurl, filename) {         // non-ie         if (!window.activexobject) {             var save = document.createelement('a');             save.href = fileurl;             save.target = '_blank';             save.download = filename || 'unknown';              var event = document.createevent('event');             event.initevent('click', true, true);             save.dispatchevent(event);             (window.url || window.webkiturl).revokeobjecturl(save.href);         }          // ie         else if ( !! window.activexobject && document.execcommand)     {             var _window = window.open(fileurl, '_blank');             _window.document.close();             _window.document.execcommand('saveas', true, filename || fileurl)             _window.close();         }      // ie 11     try {         var evt = new mouseevent('click'); } catch (e) {         window.open(fileurl, filename);  }  } 

just test image photo.jpg when done.

<button style="width: 100px; height: 50px;" onclick=savetodisk(<?php echo $imagelarge[0]; ?>http://lorempixel.com/400/200/);></button> 

onclick attribute syntax wrong. value must enclosed in double or single qoutes , contain valid javascript code.

<button style="width: 100px; height: 50px;" onclick="savetodisk('http://lorempixel.com/400/200/', 'filename.jpg')">download</button> 

here full working example.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -