uniapp download save file

download file

        // 下载文件
        downloadFile(url) {
    
    
            uni.downloadFile({
    
    
                url: url,
                success: function (res) {
    
    
                    var filePath = res.tempFilePath
                    uni.openDocument({
    
    
                        filePath: filePath,
                        showMenu: true,
                        success: function (res) {
    
    
                            console.log('打开文档成功')
                        }
                    })
                }
            })
        },
        

picture download

        // 保存图片先判断是否有相册权限
        save(url) {
    
    
            const that = this
            uni.showLoading({
    
    
                title: '正在保存图片...'
            })
            //获取用户的当前设置。获取相册权限
            uni.getSetting({
    
    
                success: (res) => {
    
    
                    //如果没有相册权限
                    if (!res.authSetting['scope.writePhotosAlbum']) {
    
    
                        //向用户发起授权请求
                        uni.authorize({
    
    
                            scope: 'scope.writePhotosAlbum',
                            success: () => {
    
    
                                //授权成功保存图片到系统相册
                                uni.downloadFile({
    
    
                                    url: url,
                                    success: function (res) {
    
    
                                        var filePath = res.tempFilePath
                                        uni.saveImageToPhotosAlbum({
    
    
                                            // 图片路径,不支持网络图片路径
                                            filePath: filePath,
                                            success: (res) => {
    
    
                                                uni.hideLoading()
                                                uni.openDocument({
    
    
                                                    filePath: filePath,
                                                    showMenu: true,
                                                    success: function (res) {
    
    
                                                        console.log('打开文档成功')
                                                    }
                                                })
                                            },
                                            fail: (res) => {
    
    
                                                uni.hideLoading()
                                                console.log(res.errMsg)
                                                uni.showToast({
    
    
                                                    title: res.errMsg
                                                })
                                            }
                                        })
                                    }
                                })
                            },
                            //授权失败
                            fail: () => {
    
    
                                uni.hideLoading()
                                uni.showModal({
    
    
                                    title: '您已拒绝获取相册权限',
                                    content: '是否进入权限管理,调整授权?',
                                    success: (res) => {
    
    
                                        if (res.confirm) {
    
    
                                            //调起客户端小程序设置界面,返回用户设置的操作结果。(重新让用户授权)
                                            uni.openSetting({
    
    
                                                success: (res) => {
    
    
                                                    console.log(res.authSetting)
                                                }
                                            })
                                        } else if (res.cancel) {
    
    
                                            uni.showToast({
    
    
                                                title: '已取消!',
                                                icon: 'none'
                                            })
                                        }
                                    }
                                })
                            }
                        })
                    } else {
    
    
                        uni.downloadFile({
    
    
                            url: url,
                            success: function (res) {
    
    
                            	// 图片临时路径
                                var filePath = res.tempFilePath
                                uni.saveImageToPhotosAlbum({
    
    
                                    // 图片路径,不支持网络图片路径
                                    filePath: filePath,
                                    success: (res) => {
    
    
                                        uni.hideLoading()
                                        uni.openDocument({
    
    
                                            filePath: filePath,
                                            showMenu: true,
                                            success: function (res) {
    
    
                                                console.log('打开文档成功')
                                            }
                                        })
                                    },
                                    fail: (res) => {
    
    
                                        uni.hideLoading()
                                        console.log(res.errMsg)
                                        uni.showToast({
    
    
                                            title: res.errMsg
                                        })
                                    }
                                })
                            }
                        })
                    }
                }
            })
        },

When downloading files with pictures and files, you need to judge which method to use first.

		toDownloadFile(url) {
    
    
            let imgType1 = ['jpg', 'jpeg', 'png']
            let type = url.split('.')[url.split('.').length - 1]
            for (let i = 0; i < imgType1.length; i++) {
    
    
                if (imgType1[i].toUpperCase() === type.toUpperCase()) {
    
    
                    this.save(url)
                    return
                }
            }
            this.downloadFile(url)
        },

Guess you like

Origin blog.csdn.net/weixin_44801790/article/details/131397913