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",
            })
        }
    });

}
相关推荐
AI英德西牛仔17 小时前
手机文心怎么导出文档?AI 导出鸭一键解决格式崩塌与批量归档难题
人工智能·智能手机·excel·deepseek·ai导出鸭
p***769817 小时前
Open Minis – 免费开源本地运行 AI Agent 智能助手|实现手机电脑运行的开源 AI Agent 平台
人工智能·智能手机·开源
xixiaoyunya20 小时前
移动办公场景下手机远程访问内网设备的技术方案选型与实践
网络·智能手机·智能路由器
CHB1 天前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark【Android版】
android·ios·uni-app
Magic-ZYJ1 天前
不登录、不联网也能用:我是如何设计一款“本地优先”iOS App 的
ios·app·iphone·研发·独立开发者·心情日记
_阿南_1 天前
flutter 展示的字体突然奔放了
android·flutter·ios
90后的晨仔2 天前
你的 iOS 最低部署版本即将不再被 App Store 接受
ios
2601_965798472 天前
Launch Your Wellness App Fast: The Truth About Meditation Source Code
前端·macos·ios·objective-c·cocoa
初级代码游戏2 天前
iOS开发 Swift 速记7:结构体和类
开发语言·ios·swift
Anhty3 天前
手机端叮咚变声器2026实测:功能表现、优缺点完整解析
功能测试·ios·智能手机·安卓