uniapp下载附件保存到手机(文件、图片)ios兼容

  • downloadFile(file),其中file为下载的文件地址
  • uni.downloadFile
  • 图片使用uni.saveImageToPhotosAlbum【安卓、ios都合适】
  • 文件使用uni.openDocument【安卓图片也可以用这个,ios会失败】
// 下载文件
export function downloadFile(file) {
    
    
    let acceptArr = ["JPG", "PNG", "JPEG"]
    const fileSuffix = file.substring(file.lastIndexOf(".") + 1).toUpperCase();
    //加载框动画
    uni.showLoading({
    
     title: '正在下载……' });

    uni.downloadFile({
    
     //只能是GET请求
        url: file, //请求地址(后台返回的码流地址)
        success: (res) => {
    
    
            //下载成功
            if (res.statusCode === 200) {
    
    
                uni.hideLoading();   //隐藏加载框
                //保存文件
                var tempFile = res.tempFilePath;
                console.log(tempFile, res, 'tempFilePath')
                if (acceptArr.indexOf(fileSuffix) >= 0) {
    
    
                    console.log('图片')
                    uni.saveImageToPhotosAlbum({
    
    
                        filePath: res.tempFilePath,
                        success: function () {
    
    
                            uni.showToast({
    
    
                                title: "保存成功",
                                icon: "none"
                            });
                        },
                        fail: function () {
    
    
                            uni.showToast({
    
    
                                title: "保存失败,请稍后重试",
                                icon: "none"
                            });
                        }
                    });
                } else {
    
    
                    console.log('文件')
                    //保存成功之后 打开文件
                    uni.openDocument({
    
    
                        filePath: tempFile,
                        showMenu: true, //微信小程序 downloadFile生成的tempFilePath为临时路径无法直接保存到手机 显示菜单设置可以手动保存到手机本地
                        fail: (e) => {
    
    
                            console.log(e, '打开失败')
                            let nowEno = uni.getSystemInfoSync().platform; //当前环境
                            console.log(e, '打开失败', nowEno)
                            if (nowEno == 'ios') {
    
     //ios打开临时路径文件失败 设置ios环境下读取临时路径文件可以打开
                                uni.getFileSystemManager().readFile({
    
    
                                    filePath: tempFile,
                                    success: res => {
    
    
                                        var filebuffer = res.data
                                        return filebuffer
                                    },
                                    fail: console.error
                                })
                            } else {
    
    
                                uni.showToast({
    
    
                                    title: '打开失败'
                                })
                            }

                        }
                    })

                }
            }
        },
        fail: (e) => {
    
    
            console.log(e, '文件下载失败')
            uni.showToast({
    
    
                title: '文件下载失败',
                icon: "none",
            })
        }
    });

}

猜你喜欢

转载自blog.csdn.net/sjp991012/article/details/133949442