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

  • downloadFile(file),其中file为下载的文件地址
  • uni.downloadFile
  • 图片 使用uni.saveImageToPhotosAlbum【安卓、ios都合适】
  • 文件 使用uni.openDocument【安卓图片也可以用这个,ios会失败】
javascript 复制代码
// 下载文件
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",
            })
        }
    });

}
相关推荐
蓝帆傲亦30 分钟前
支付宝小程序性能暴增秘籍:UniApp项目极限优化全攻略
小程序·uni-app
七夜zippoe3 小时前
脉向AI|当豆包手机遭遇“全网封杀“:GUI Agent是通向AGI的必经之路吗?
人工智能·ai·智能手机·agent·gui
crmscs3 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
lichensun3 小时前
2026美赛A题智能手机电池耗电建模—思路全解析(含具体模型和代码)
数学建模·智能手机
IT技术分享社区3 小时前
手机投屏到电视上,4种不同的方法,一起来了解一下
智能手机·投屏·手机技巧
巴德鸟3 小时前
华为手机鸿蒙4回退到鸿蒙3到鸿蒙2再回退到EMUI11 最后关闭系统更新
华为·智能手机·harmonyos·降级·升级·回退·emui
GR2342343 小时前
2025年影视仓TV+手机官方版 内置地址源支持高清直播
java·智能手机·软件
灿灿数模(加更版)3 小时前
2026美赛 MCM Problem A 智能手机电池耗电建模思路分析,完整论文
智能手机
普通网友3 小时前
苹果笔记本(Mac)连接手机完全指南
macos·智能手机
锐意无限4 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift