实现在uniapp微信小程序中点击按钮,保存图片到本地相册,具体实现方法:
template中
<button class="haibaobtn" @click="handledownload">保存图片</button>
js中
- handledownload(){
- const that = this
- uni.downloadFile({
- url: '图片地址',
- success: res => {
- if (res.statusCode === 200) {
- uni.saveImageToPhotosAlbum({
- filePath: res.tempFilePath,
- success: function() {
- uni.showToast({
- title: '保存成功',
- duration: 2000
- })
-
- },
- fail: function() {
- uni.showToast({
- title: '保存失败',
- duration: 2000
- })
-
- }
- })
- } else {
- uni.showToast({
- title: '保存失败',
- duration: 2000
- })
- }
- }
- })
- }
手机运行: