学校网站建,广州网站建设广州,佛山企业一般在哪网站发布消息,丰都网站建设公司uniapp如何下载文件、保存、打开文件 时光荏苒#xff0c;2024即将过去#xff01; 迈向2025#xff0c;祝大家新的一年工作顺利、万事如意#xff0c;少一点BUG#xff0c;涨一点工资…↖(ω)↗ 文章目录 uniapp如何下载文件、保存、打开文件下载文件保存并打开文件处理 …uniapp如何下载文件、保存、打开文件 时光荏苒2024即将过去 迈向2025祝大家新的一年工作顺利、万事如意少一点BUG涨一点工资…↖(ω)↗ 文章目录 uniapp如何下载文件、保存、打开文件下载文件保存并打开文件处理 iOS 打开文件可能失败问题相关API返回数据格式下载成功保存文件成功拷贝目录成功 相关Api uni.downloadFileuni.saveFile5 API 下载文件
通过 uni.downloadFile 结合 uni.saveFile 及 5 Api 实现例导出 Excel文件
downloadFile() {// 下载文件资源到本地let header {Authorization: getToken()}uni.downloadFile({url: xxx/export,header,success: (res) {console.log(下载成功, res);if (res.statusCode 200) {this.saveFile(res.tempFilePath);}},fail: (err) {uni.showToast({title: 下载失败,icon: error})}});
}保存并打开文件
保存文件到本地
saveFile(tempFilePath) {uni.saveFile({tempFilePath: this.fileNameEscape(tempFilePath),success: (res) {console.log(保存文件成功, res);// 需要保存的文件的临时路径let tempFilePath res.savedFilePath;let fileName Smile_${new Date().getTime()}.xlsx; // 目标文件名// 通过URL参数获取目录对象或文件对象plus.io.resolveLocalFileSystemURL(tempFilePath, (entry) {plus.io.resolveLocalFileSystemURL(_doc/, (root) {// 创建或打开当前目录下指定的文件root.getFile(fileName, {create: true}, (file) {// 删除、拷贝目录file.remove(() {entry.copyTo(root, fileName, (entry) {console.log(拷贝目录成功, entry.fullPath);// 调用第三方程序打开指定的文件plus.runtime.openFile(entry.fullPath);}, (err) {console.log(拷贝目录失败, err);});});}, (err) {console.log(获取文件失败, err);});});});},fail: (err) {console.log(保存文件失败, err);}});
}处理 iOS 打开文件可能失败问题
下载文件名中包含中文字符时会失败情况
fileNameEscape(filename) {if (uni.getSystemInfoSync().platform ios) {filename escape(filename);}return filename;
},相关API返回数据格式
下载成功
{tempFilePath: _doc/uniapp_temp_1735639708906/download/export.json,statusCode: 200,errMsg: downloadFile:ok
}保存文件成功
{errMsg: saveFile:ok,savedFilePath: _doc/uniapp_save/17356398268860.json
}拷贝目录成功
/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/Smile_1735639826926.xlsx