downloadFile(file)
,其中file
为下载的文件地址uni.downloadFile
- 图片使用
uni.saveImageToPhotosAlbum
【安卓、ios都合适】 - 文件使用
uni.openDocument
【安卓图片也可以用这个,ios会失败】
export function downloadFile(file) {let acceptArr = ["JPG", "PNG", "JPEG"]const fileSuffix = file.substring(file.lastIndexOf(".") + 1).toUpperCase();uni.showLoading({ title: '正在下载……' });uni.downloadFile({ 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, fail: (e) => {console.log(e, '打开失败')let nowEno = uni.getSystemInfoSync().platform; console.log(e, '打开失败', nowEno)if (nowEno == 'ios') { uni.getFileSystemManager().readFile({filePath: tempFile,success: res => {var filebuffer = res.datareturn filebuffer},fail: console.error})} else {uni.showToast({title: '打开失败'})}}})}}},fail: (e) => {console.log(e, '文件下载失败')uni.showToast({title: '文件下载失败',icon: "none",})}});}