如何防止开放"另存为"使用javascript下载图像时的对话框

时间:2015-10-21 06:09:37

标签: javascript jquery google-chrome-extension html5-canvas

我只是想通过javascript下载网页的所有图片,但却陷入困境。当我下载所有图像"另存为"对话框打开,因为我想一次下载所有图像因此很多"另存为"对话框打开并使浏览器崩溃,从而导致系统崩溃。

请注意: - 我必须通过在网页中注入javascript来完成此操作。因此onload插件无法正常工作

我的js代码是

for (i = 0; i < all_img_tags.length; i++) {
     console.log(all_img_tags[i].getAttribute('src'));  
     if(all_img_tags[i].width>0){
         var filename = 'file_'+i;
         var downloadLink = document.createElement("a");
         downloadLink.download = filename;
         downloadLink.href = all_img_tags[i].getAttribute('src');
         downloadLink.click();
     }
} 

我也试过这个

if(all_img_tags[i].width>0){    
    convertImgToBase64URL(all_img_tags[i].getAttribute('src'), function(base64Img){
        var blob = dataUriToBlob(base64Img);
        url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
     });
}

function convertImgToBase64URL(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS'),
        ctx = canvas.getContext('2d'), dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
   img.src = url;
}

0 个答案:

没有答案