• 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",
                })
            }
        });
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    go语言学习笔记
    react-starter脚手架搭建过程
    Spring MVC
    SQLite 学习笔记2 - 常用命令和示例
    什么是哈希表
    某个订单项目记录,涉及MQ消息处理、分布式问题、幂等性等问题解决设计
    【开题报告】基于SpringBoot的二手汽车交易平台的设计与实现
    MySQL MHA高可用配置及故障切换
    【Linux常用命令14】Linux系统监控常用命令
    Java开发学习(一)----初识Spring及其核心概念
  • 原文地址:https://blog.csdn.net/sjp991012/article/details/133949442