1.下载文件到本地
// 下载文件到本地XMLHttpRequest
// fileUrl为网络图片链接
export function xmlHttpRequestFile(fileUrl) {
if(!fileUrl) {
return uni.showToast({
title: '文件路径为空',
icon: 'none'
})
}
uni.showLoading({
title: '请稍等···',
mask: true
});
return new Promise(function (resolve, reject) {
let xml = new XMLHttpRequest();
xml.open('GET', fileUrl, true);
xml.responseType = 'blob';
xml.onload = function () {
uni.hideLoading();
let tempFilePath = window.URL.createObjectURL(xml.response);
// let a = document.createElement('a');
// a.href = tempFilePath;
// a.download = 'download';
// a.click();
// 数据处理完成
resolve(tempFilePath);
}
xml.send();
});
}
2.调用方式
// filePath为获取到的本地链接
const filePath = await xmlHttpRequestFile(fileUrl);