This article mainly introduces the code for downloading images to the local via js in detail. It has certain reference value. Interested friends can refer to it.
The code is as follows:
$(function(){ //二维码 (function(){ var img_src = $('.qr_img')[0].src; if(browserIsIe()){//假如是ie浏览器 $('.down_qr').on('click',function(){ img_src = $('.qr_img')[0].src; DownLoadReportIMG(img_src); }); }else{ $('.down_qr').attr('download',img_src); $('.down_qr').attr('href',img_src); $('.sutmit_btn').on('click',function(){ $('.down_qr').attr('download',img_src); $('.down_qr').attr('href',img_src); }); } })(); }); function DownLoadReportIMG(imgPathURL) { //如果隐藏IFRAME不存在,则添加 if (!document.getElementById("IframeReportImg")) $('<iframe style="display:none;" id="IframeReportImg" name="IframeReportImg" onload="DoSaveAsIMG();" width="0" height="0" src="about:blank"></iframe>').appendTo("body"); if (document.all.IframeReportImg.src != imgPathURL) { //加载图片 document.all.IframeReportImg.src = imgPathURL; } else { //图片直接另存为 DoSaveAsIMG(); } } function DoSaveAsIMG() { if (document.all.IframeReportImg.src != "about:blank") window.frames["IframeReportImg"].document.execCommand("SaveAs"); } //判断是否为ie浏览器 function browserIsIe() { if (!!window.ActiveXObject || "ActiveXObject" in window) return true; else return false; }
Summary:
javascript does not have permission to operate local files and can only use .net, Only back-end languages such as php can be used. After submitting the image, return a download address and window.open can automatically download it.
Related recommendations:
Two ways to download images with node.js
js download and modify files Famous example tutorial
javascript - Use js to download web pages?
The above is the detailed content of How to download images to local using js. For more information, please follow other related articles on the PHP Chinese website!