Home  >  Article  >  Web Front-end  >  How to download images to local using js

How to download images to local using js

墨辰丷
墨辰丷Original
2018-05-09 15:46:475752browse

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"))  
        $(&#39;<iframe style="display:none;" id="IframeReportImg" name="IframeReportImg" onload="DoSaveAsIMG();" width="0" height="0" src="about:blank"></iframe>&#39;).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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn