Front-end non-homologous images download

Since then daily development, there is often a company's image server. When front-end requirements to achieve download pictures often have non-homologous issues.

Non-homologous use download preview property by default instead of downloading.

Since the image server often allow cross-domain, so we are here with the way ajax get request, obtain pictures blob file, and then by

window.URL.createObjectURL

(Or blob file designed to be read as a url may be img, video, audio recognition is displayed url)

1  function downloadImg (src, name) {
 2          var the X-= new new the XMLHttpRequest (); 
       // disable browser cache; otherwise, an error will be reported across domains
3 x.open ( "GET", src + + 't =?' New new a Date () .getTime (), to true ); . 4 x.responseType = 'BLOB' ; . 5 x.onload = function (E) { . 6 var URL = window.URL.createObjectURL (x.response) . 7 var A = document.createElement ( 'A' ); . 8 a.href = URL . 9 a.download = name 10 a.click() 11 } 12 x.send(); 13 }
<a href="javascript:;" id="download" onclick='downloadImg(url,name)'>下载</a>

  

 

Guess you like

Origin www.cnblogs.com/yihuite-zch/p/11445733.html