When using uni-app to develop a mobile app, I encountered the following requirements:
There is a path of a picture on the web page. On the web side, click on this path to jump to the download path of the picture and download the picture. But it is not used on the mobile terminal.
I want to save this picture in an album.
method:
let url = '图片地址'
// web端下载图片
// #ifdef H5
window.location.href = url
// #endif
// 移动端下载图片
// #ifdef APP-PLUS
uni.downloadFile({
url,
success: res => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({ title: '保存成功' })
},
fail: function() {
uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
}
});
} else uni.showToast({ title: '下载失败', icon: 'none' })
}
})
// #endif
The above method can achieve my needs.