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

}
相关推荐
游戏开发爱好者81 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
黑码哥1 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
2501_915106323 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
熊猫钓鱼>_>3 小时前
移动端开发技术选型报告:三足鼎立时代的开发者指南(2026年2月)
android·人工智能·ios·app·鸿蒙·cpu·移动端
宠友信息4 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”5 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
徐同保1 天前
通过ip访问nginx的服务时,被第一个server重定向了,通过设置default_server解决这个问题
ios·iphone
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview