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

}
相关推荐
wanhengidc3 小时前
云手机 高振畅玩不踩坑
运维·服务器·安全·web安全·智能手机
SmartRadio4 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
非凡ghost8 小时前
可拓浏览器:给手机浏览器装上“外挂“!2W+拓展+AI搜索,玩出无限可能!
windows·智能手机·音视频·firefox
心中无石马8 小时前
uniapp引入tailwindcss4.x
前端·css·uni-app
fix一个write十个8 小时前
【uniApp开发】微信小程序 web-view 内嵌 H5 跳转支付踩坑实录
微信小程序·uni-app
美狐美颜SDK开放平台9 小时前
多场景美颜SDK解决方案:直播APP(iOS/安卓)开发接入详解
android·人工智能·ios·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
wuxianda103011 小时前
苹果App上架4.3a被拒解决方案汇报总结
ios·uni-app·objective-c·cocoa·苹果上架·4.3a
SmartRadio13 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信 (采用Arduino代码框架)
开发语言·智能手机·esp32·长距离wifi
AI刀刀16 小时前
手机AI怎么导出pdf
人工智能·ai·智能手机·pdf·deepseek·ds随心转