js点击保存图片

 
方法一:
 <img :src="codeIMG" alt="二维码图片" style="width:60%;" />
    <span @click="downloadCodeImg()">点击保存</span>
   
codeIMG: require("../../assets/image/222.png")
 
downloadCodeImg() {
      console.log("下载图片");
      var a = document.createElement("a");
      a.download = name || "pic"; // 设置图片地址
      a.href = this.codeIMG;
      a.click();
    },
 
 
 
方法二:
 
  <img src="../../assets/image/business.png" alt="" ref="picurl" id="picurl"/>

    <a href="#" @click="savePic">保存到相册</a>
 
 
 
    savePic() {
      var picurl = this.$refs.picurl.src;

      //alert(picurl);
      this.savePicture(picurl);
    },
    savePicture(Url) {
      console.log(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(/(.*\/)*([^.]+.*)/gi, "$2").split("?")[0];
      console.log(a);
      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);
      console.log(URL.revokeObjectURL(url));
      URL.revokeObjectURL(url);
    },
posted @ 2020-06-11 11:12  web小超  阅读(1666)  评论(0编辑  收藏  举报