uniapp微信小程序做下载(保存)图片需求

downImg() {
    uni.downloadFile({
        url: this.imgUrl,//后端返回的图片地址
        success: function(res) {
            if (res.statusCode === 200) {
                uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function() {
                        uni.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        });
                    },
                    fail: function(err) {
                        // console.log(err);
                        wx.showToast({
                            title: '保存失败',
                            icon: 'none',
                            duration: 2000
                        });
                    }
                });
            } else {
                // console.log('下载失败');
            }
        },
        fail: function(err) {
            // console.log(err);
        }
    });
}

根据提供的代码,uniapp微信小程序下载图片的方法有多种实现方式。以下是其中几种常见的方法: 方法一: 可以使用uni.downloadFile()方法下载图片,然后使用uni.saveImageToPhotosAlbum()方法保存图片到本地相册。代码示例如下: ```javascript 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 }) } } }) } ``` 方法二: 可以使用uni.downloadFile()方法下载图片,然后使用uni.openDocument()方法打开文档来查看下载图片。代码示例如下: ```javascript download(i) { uni.downloadFile({ url: this.mate[i].path, success: function(res) { var filePath = res.tempFilePath; uni.openDocument({ filePath: filePath, showMenu: true, success: function(res) { console.log('打开文档成功'); } }); }, fail: (err) => { console.log(err); uni.showToast({ icon: 'none', mask: true, title: '失败请重新下载' }); }, }); } ``` 方法三: 与方法二类似,可以使用uni.downloadFile()方法下载图片,然后使用uni.saveFile()方法将文件保存到本地,最后使用uni.openDocument()方法打开保存图片。代码示例如下: ```javascript download(i) { uni.downloadFile({ url: this.mate[i].path, success: function(data) { if (data.statusCode === 200) { uni.saveFile({ tempFilePath: data.tempFilePath, success: function(res) { uni.showToast({ icon: 'none', mask: true, title: '文件已保存:' + res.savedFilePath, duration: 3000 }); setTimeout(() => { uni.openDocument({ filePath: res.savedFilePath, success: function(res) { console.log('打开文档成功'); } }); }, 2000); } }); } }, fail: (err) => { console.log(err); uni.showToast({ icon: 'none', mask: true, title: '失败请重新下载' }); }, }); } ``` 以上是uniapp微信小程序下载图片的几种常见实现方法。根据实际需求和代码逻辑,可以选择适合的方法进行实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [在uniapp微信小程序保存图片到本地相册](https://blog.csdn.net/jayusmazyyolk/article/details/126532923)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp下载文件 例(Word)以及下载其他文件,Word文件的转发,保存,查看](https://blog.csdn.net/weixin_56718509/article/details/124615998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值