特易招,招聘小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.7 KiB

const fsm = wx.getFileSystemManager();
const FILE_BASE_NAME = 'tmp_base64src';
const base64src = function(base64data) {
return new Promise((resolve, reject) => {
// 添加时间戳生成唯一文件名
const timestamp = Date.now();
const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}_${timestamp}.png`;
const buffer = wx.base64ToArrayBuffer(base64data);
fsm.writeFile({
filePath,
data: buffer,
encoding: 'binary',
success() {
resolve(filePath);
},
fail() {
reject(new Error('ERROR_BASE64SRC_WRITE'));
},
});
});
};
const unlink = function() {
return new Promise((resolve, reject) => {
// 获取临时文件列表
fsm.readdir({
dirPath: wx.env.USER_DATA_PATH,
success: (res) => {
// 删除所有tmp_base64src开头的临时文件
const promises = res.files
.filter(file => file.startsWith(FILE_BASE_NAME))
.map(file => {
return new Promise((res, rej) => {
fsm.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${file}`,
success: res,
fail: rej
});
});
});
Promise.all(promises)
.then(resolve)
.catch(reject);
},
fail: reject
});
});
};
export default {
base64src,
unlink,
};