Base64图片的下载
Page({
data: {
imageurl: ''
},
onLoad(options) {
console.log(options);
let url = options.url
let quarterLength = Math.ceil(url.length / 4);
let str1 = url.substring(0, quarterLength);
let str2 = url.substring(quarterLength, quarterLength * 2);
let str3 = url.substring(quarterLength * 2, quarterLength * 3);
let str4 = url.substring(quarterLength * 3);
this.setData({
imageurl: (str1 + str2 + str3 + str4).replace(/[\r\n]/g, '')
})
},
base64ToImageFile(base64Data, callback) {
const fs = wx.getFileSystemManager();
const timestamp = new Date().getTime();
const filePath = `${wx.env.USER_DATA_PATH}/image_${timestamp}.png`;
base64Data = base64Data.replace(/^data:image\/png;base64,/, '');
try {
const buffer = wx.base64ToArrayBuffer(base64Data);
fs.writeFile({
filePath: filePath,
data: buffer,
encoding: 'binary',
success: () => {
callback(filePath);
},
fail: (err) => {
console.error('保存Base64图片到本地失败:', err);
}
});
} catch (e) {
console.error('Base64转换失败:', e);
}
},
saveBase64ImageToPhotosAlbum(base64Data) {
wx.showLoading({
title: '正在保存...',
})
wx.getSetting({
withSubscriptions: true,
success: res => {
console.info(res.authSetting);
if (res.authSetting['scope.writePhotosAlbum']) {
this.base64ToImageFile(base64Data, (filePath) => {
wx.saveImageToPhotosAlbum({
filePath: filePath,
success: (res) => {
wx.hideLoading()
setTimeout(()=>{
wx.showToast({
title: '保存成功',
icon: 'success',
});
},500)
setTimeout(()=>{
wx.navigateBack()
},2500)
},
fail: (err) => {
wx.hideLoading()
setTimeout(()=>{
wx.showToast({
title: '保存失败',
icon: 'error'
});
},500)
setTimeout(()=>{
wx.navigateBack()
},2500)
console.error('保存图片到相册失败:', err);
}
});
});
} else {
wx.hideLoading()
wx.showModal({
cancelColor: 'cancelColor',
title: '提示',
content: '请开启相册访问权限',
success: res => {
if (res.confirm) {
wx.openSetting({
withSubscriptions: true,
})
} else if (res.cancel) {
setTimeout(()=>{
wx.showToast({
title: '保存失败',
icon: 'error'
});
},500)
setTimeout(()=>{
wx.navigateBack()
},2500)
console.log('点击了取消,返回上层');
}
}
})
}
}
})
},
onReady() {
},
onShow() {
this.saveBase64ImageToPhotosAlbum(this.data.imageurl.split(',')[1])
},
onHide() {
},
onUnload() {
},
onPullDownRefresh() {
},
onReachBottom() {
},
onShareAppMessage() {
}
})
在线图片下载到相册(包含进度条)
downloadImageClick(e) {
wx.getSetting({
withSubscriptions: true,
success: res => {
console.info(res.authSetting);
if (res.authSetting['scope.writePhotosAlbum']) {
const downloadTask = wx.downloadFile({
url: e.currentTarget.dataset.url,
success: res => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: res => {
console.info(res);
wx.showToast({
title: "保存成功",
icon: 'success'
});
},
fail: res => {
wx.showToast({
title: '保存失败',
icon: error
})
}
})
},
fail(err){
wx.showToast({
title: '保存失败,建议复制链接请前往浏览器下载,或者重新生成视频',
icon:"none"
})
}
});
downloadTask.onProgressUpdate((res) => {
wx.showLoading({
title: `下载中... ${res.progress}%`,
});
});
} else {
wx.showModal({
cancelColor: 'cancelColor',
title: '提示',
content: '请开启相册访问权限',
success: res => {
if (res.confirm)
wx.openSetting({
withSubscriptions: true,
})
}
})
}
}
})
},
在线视频下载到相册(包含进度条)
downloadVideoClick(e) {
console.log(e.currentTarget.dataset.url);
wx.getSetting({
withSubscriptions: true,
success: res => {
console.info(res.authSetting);
if (res.authSetting['scope.writePhotosAlbum']) {
const downloadTask = wx.downloadFile({
url: e.currentTarget.dataset.url,
success: res => {
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: res => {
console.info(res);
wx.showToast({
title: "保存成功",
icon: 'success'
});
},
fail: res => {
wx.showToast({
title: '保存失败',
icon: "error"
})
}
})
},
fail(err){
console.log(err);
wx.showToast({
title: '保存失败,建议复制链接请前往浏览器下载,或者重新生成视频',
icon:"none"
})
}
});
downloadTask.onProgressUpdate((res) => {
wx.showLoading({
title: `下载中... ${res.progress}%`,
});
});
} else {
wx.showModal({
cancelColor: 'cancelColor',
title: '提示',
content: '请开启相册访问权限',
success: res => {
if (res.confirm)
wx.openSetting({
withSubscriptions: true,
})
}
})
}
},
fail(err){
console.log(err);
}
})
},