赞
踩
效果图
可直接复用,接口地址换自己的
<template> <div> <div class="box"> <img :src="sss" alt="" /> <button @click="ermxz">保存二维码</button> </div> </div> </template> <script> export default { data() { return { imgSrc: "", srcc: "data:image/png;base64,", sss: "", number: "", }; }, mounted() { this.erm(); }, methods: { //转格式并渲染到界面 erm() { let token = wx.getStorageSync("authToken"); //获取token var that = this; wx.request({ url: "接口地址", method: "get", header: { Hrsyabc: token, }, responseType: "arraybuffer", //此处是请求文件流,必须带入的属性 success(res) { if (res.statusCode === 200) { var imgSrc = wx.arrayBufferToBase64(res.data); //二进制流转为base64编码 (that.imgSrc = imgSrc), (that.sss = that.srcc + that.imgSrc); var save = wx.getFileSystemManager(); var number = Math.random(); that.number = number; save.writeFile({ filePath: wx.env.USER_DATA_PATH + "/pic" + number + ".png", data: imgSrc, encoding: "base64", success: (res) => { console.log(that.srcc); }, fail: (err) => { console.log(err); }, }); } }, fail: function (error) { console.log(error); }, }); }, //点击保存到手机相册 ermxz() { wx.saveImageToPhotosAlbum({ //保存为png格式到相册 filePath: wx.env.USER_DATA_PATH + "/pic" + this.number + ".png", success: function (res) { wx.showToast({ title: "下载成功", icon: "none", duration: 2000, //提示的延迟时间,单位毫秒,默认:1500 }); }, fail: function (err) { console.log(err); }, }); }, }, }; </script> <style> .box { width: 300px; height: 300px; } .box img { width: 100%; height: 100%; } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。