Study notes-how uni-app downloads and saves pictures of the network path on the mobile terminal

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.

Guess you like

Origin blog.csdn.net/qq_41339126/article/details/113397411