微信小程序点击按钮,将图片下载到本地

前言:

最近在公司完成一个小程序的时候需要实现一个功能:点击按钮获取用户相册权限,将图片下载到用户本地相册,经过了好几次的尝试最终算是实现了。将总结的经验在这里分享给小伙伴们。

实现方式:

html 复制代码
//.wxml文件
<view class="canvas-box">
//注意:一定要添加:type="2d"
  <canvas id="posterCanvas"canvas-id="posterCanvas" type="2d"></canvas>
  <view class="upload_btn" bindtap="onSaveSuccess"></view>
</view>
javascript 复制代码
 //.js文件
  data: {
    urlSrc: "",
  },
// 保存图片
    onSaveSuccess() {
      const query = wx.createSelectorQuery().in(this);
      query
        .select("#posterCanvas")
        .fields({ node: true, size: true })
        .exec((res) => {
          if (res[0] && res[0].node) {
            const canvas = res[0].node;
            // 将 canvas 内容导出为临时文件
            wx.canvasToTempFilePath({
              canvas,
              success: (res) => {
                if (res.tempFilePath) {
                  this.setData({
                    urlSrc: res.tempFilePath,
                  });
                  this.saveCard(res.tempFilePath);
                } else {
                  wx.showToast({
                    title: "导出失败,请重试",
                    icon: "none",
                  });
                }
              },
              fail: (err) => {
                wx.showToast({
                  title: "导出失败,请重试",
                  icon: "none",
                });
              },
            });
          } else {
            wx.showToast({
              title: "未找到 canvas 节点",
              icon: "none",
            });
          }
        });
    },
    saveCard: function (img) {
      let that = this;
      var imgSrc = img;
      //获取相册授权
      wx.getSetting({
        success(res) {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            wx.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                that.img(imgSrc);
              },
            });
          } else {
            that.img(imgSrc);
          }
        },
      });
    },
    img: function (imgSrc) {
      var imgSrc = imgSrc;
      // wx.downloadFile({
      //   url: imgSrc,
      //   success: function (res) {
      wx.saveImageToPhotosAlbum({
        filePath: imgSrc,
        success: function (data) {
          wx.showToast({
            title: "保存成功",
            duration: 2000,
          });
        },
        fail: function (err) {
          if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
            wx.openSetting({
              success(settingdata) {
                if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                  wx.showToast({
                    title: "图片已保存",
                    icon: "none",
                    duration: 2000,
                  });
                  console.log(
                    "获取权限成功,给出再次点击图片保存到相册的提示。"
                  );
                } else {
                  console.log("获取权限失败,给出不给权限就无法正常使用的提示");
                }
              },
            });
          }
        },
      });
      //   },
      //   fail: function (err) {
      //     console.log(err, "进入wx.downloadFile错误分支");
      //   },
      // });
    },

注意事项:

不使用wx.downloadFile方法的原因是:使用 wx.canvasToTempFilePath方法将会导出一个http://temp....png的图片,使用wx.downloadFile方法将会报错:downloadFile:fail url not in domain list, 关于解决这个报错请看我的另一篇博客:解决微信小程序下载图片报错:downloadFile:fail url not in domain list。众所周知,微信公众平台的downloadFile合法域名只能配置以https://开头的域名,既然已经获得了这个文件地址就可以直接调用 wx.saveImageToPhotosAlbum方法,将其直接保存在用户本地相册

相关推荐
2501_9160074719 小时前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063219 小时前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS1 天前
供求求购供应发布VIP会员抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·供求求购供应发布
毕设源码-朱学姐1 天前
【开题答辩全过程】以 基于微信小程序旅游疫情防控管理系统为例,包含答辩的问题和答案
微信小程序·小程序·旅游
weixin_lynhgworld1 天前
陪诊小程序系统开发:开启就医陪伴新体验 [特殊字符]
小程序
weixin_lynhgworld1 天前
旧物回收小程序:让闲置物品焕发新生 ✨
java·开发语言·小程序
一点晖光1 天前
ios底部按钮被挡住
前端·ios·微信小程序
前端程序猿之路2 天前
基于扣子(Coze)工作流 API 的微信小程序开发实践总结
前端·微信小程序·小程序·大模型·api·ai编程·扣子
德育处主任2 天前
在小程序做海报的话,Painter就很给力
前端·微信小程序·canvas