WeChat Applet
Mini Program Development
Introduction to the method of saving base64 images to the album in the mini programIntroduction to the method of saving base64 images to the album in the mini program
This article brings you an introduction to the method of saving base64 images to the album in the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Authorization acquisition
1. Related api
wx.getSetting
wx.authorize
2. The authorization acquisition process is generally
Whether you have the permission——> If not——> Bring up the authorization pop-up window——> Agree——> Use the relevant API
(If the user refuses authorization, you can use wx.opensetting to guide the user to authorization Setting page authorization)
3. Code implementation
static async weAuthCheck(type = 'address') {
let resGetting = await new Promise((resolve, reject) => {
wepy.getSetting({
success: res => {
// console.log(res, 'getsetting')
if (res.authSetting.hasOwnProperty(`scope.${type}`) && res.authSetting[`scope.${type}`]) {
resolve({
succeeded: true
})
} else {
wepy.authorize({
scope: `scope.${type}`,
success: () => {
resolve({
succeeded: true
})
},
fail: err => {
// console.log(err, 'errrrr')
resolve({
succeeded: false,
err: err
})
}
})
}
},
fail: err => {
resolve({
succeeded: false,
err: err
})
}
})
})
console.log('getSetting res: \n', resGetting)
return resGetting
}
2. Writing temporary files
1. Related api
File system
writeFile
2. The parameter encoding is used to describe the format of the written parameter data. It does not mean that the data is written in the form of encoding. Here we should specify encoding as base64
3, code implementation
// 先获得一个实例 this.fileManager = wx.getFileSystemManager()
this.fileManager.writeFile({
filePath: `${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
data: data,
encoding: 'base64',
success: res => {
console.log('res: \n:', res)
},
fail: res => {
console.log(res)
}
})
3. Format string
1, format of base64 string : "data:image/png;base64,...", the paragraph before the comma is the format description, which is used to indicate that the subsequent content format is the base64 format of the image format png.
2. If you directly pass in the entire string of characters, although it can be saved successfully, it will cause an image file format error. So do another cutting operation
let startIdx = this.qrcode.indexOf('base64,') + 7
Four. Complete implementation
async onTapSaveQrcode() {
let startIdx = this.qrcode.indexOf('base64,') + 7
let resCheck = await this.$weAuthCheck('writePhotosAlbum')
let timestamp = new Date().getTime()
let self = this
if (resCheck.succeeded) {
wepy.showLoading()
this.fileManager.writeFile({
filePath: `${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
data: this.qrcode.slice(startIdx),
encoding: 'base64',
success: res => {
console.log('res: \n:', res)
wx.saveImageToPhotosAlbum({
filePath: `${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
success: res => {
self.$emit('save-qrcode-success')
wepy.showToast({
title: '保存成功'
})
},
fail: err => {
console.log(err)
if (!err.errMsg.includes('cancel')) {
wepy.showToast({
title: err.errMsg,
icon: 'none'
})
}
},
complete: () => {
wepy.hideLoading()
}
})
},
fail: res => {
wepy.hideLoading()
console.log(res)
}
})
}
}
The above is the detailed content of Introduction to the method of saving base64 images to the album in the mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment




