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

}
相关推荐
小徐_233314 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
宸翰2 天前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
天桥吴彦祖2 天前
判断iOS如何监听手机屏幕是否锁屏
ios
敲代码的鱼3 天前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹3 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹3 天前
腾讯云 TRTC UniApp SDK 从入门到上线
前端·vue.js·uni-app
时光足迹3 天前
uni-app 里把加密视频嵌入页面播放?我分析了 4 种方案,只有 1 种接近完美
前端·vue.js·uni-app
时光足迹3 天前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app
时光足迹3 天前
极光推送全攻略(下):uni-app 代码实现与 iOS 排查实战
vue.js·ios·uni-app
时光足迹3 天前
极光推送全攻略(上):被iOS证书折磨了三天,我写了一份前端也能看懂的避坑指南
前端·ios·uni-app