zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js 图片保存至手机相册

JS手机 图片 保存 相册
2023-09-11 14:19:38 时间

1.网络地址进行保存

var triggerEvent = "touchstart";  //指定下载方式
    function savePicture(Url) {
        var blob = new Blob([''], { type: 'application/octet-stream' });
        var url = URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = Url;
        a.download = Url.replace(/(.*\/)*([^.]+.*)/ig, "$2").split("?")[0];
        var e = document.createEvent('MouseEvents');
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        a.dispatchEvent(e);
        URL.revokeObjectURL(url);
    }
savePicture(Url); 
// Url 为图片的服务器地址  http://****.com/cwap/images/userqrcode/06144477550415791.pngfinal.png

 

 

2.临时地址也可以保存: blob:http://localhost:8080/d08f95f2-06da-4f9c-864e-c00079fc1c6f
var file = files[0];//上传控件选择的文件对象
var url = window.URL.createObjectURL(file);//用此方法有兼容问题
savePicture(Url); 


//下面是兼容方法

//获取图片路径
function getObjectURL (file){
  let url = null ;
  if (window.createObjectURL!=undefined) { // basic
    url = window.createObjectURL(file) ;
  } else if (window.URL!=undefined) { // mozilla(firefox)
    url = window.URL.createObjectURL(file) ;
  } else if (window.webkitURL!=undefined) { // webkit or chrome
    url = window.webkitURL.createObjectURL(file) ;
  }
  return url ;
}

 

 

 

转: https://blog.csdn.net/weixin_43271750/article/details/97639465