需求:
实现下载文件
uni.downloadFile({
// 注意:url是完整的下载地址,并不仅仅是api
// 例如:http://192.168.xx.xx:8080/manange/xx/xx/?file=xxxxxxxx
url:
getBaseUrl("") +
"/manage/manarchive/fileDownload" +
// 参数
turnObjToUrl({
technical: data.path,
}),
header: {
"Content-Type": "application/json",
Authorization:
uni.getStorageSync("token_type") +
" " +
uni.getStorageSync("access_token"),
},
success: (data) => {
// 返回结果示例
// {
// errMsg: "downloadFile:ok",
// statusCode: 200,
// tempFilePath: "blob:http://localhost:8080/80fe5aaf-a90f-4a77-bb37-acb635665444"
// }
if (data.statusCode === 200) {
uni.showToast({
icon: "none",
mask: true,
title: "下载成功!",
duration: 2000,
});
setTimeout(() => {
// 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
uni.openDocument({
// tempFilePath接口返回的临时下载路径
filePath: this.fileNameEscape(data.tempFilePath),
success: function (res) {
console.log('打开文档成功');
},
fail() {
uni.showToast({
icon: "none",
mask: true,
title: "暂不支持此类型",
duration: 2000,
});
},
});
}, 2 * 1000);
} else {
console.log("data.statusCode", data.statusCode);
}
},
fail: (err) => {
uni.hideLoading();
console.log(err);
uni.showToast({
icon: "none",
mask: true,
title: "下载失败,请重试!",
});
},
});
/**
* ios可能显示不出来,可以在IOS端下进行单独处理,使用escape
* ios下文件名中文处理
* @param {String} filename
*/
fileNameEscape(filename) {
if (uni.getSystemInfoSync().platform == "ios") {
return escape(filename);
}
return filename;
}
版权声明:本文为qq_43033251原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。