uniapp H5/APP 导出流文件
fequestFile(_url){
const token = uni.getStorageSync('token');
uni.showLoading({
title: '加载中...'
});
uni.downloadFile({
url: _url,
header: {
Authorization: token
},
responseType: 'arraybuffer',
success: (res) => {
uni.hideLoading();
if (res.statusCode == 200) {
const tempFilePath = res.tempFilePath;
uni.saveFile({
tempFilePath,
success: (res2) => {
this.downloadFile(res2.savedFilePath);
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: res.errMsg,
icon: "none"
});
}
});
} else {
uni.showToast({
title: res.errMsg,
icon: "none"
});
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: res.errMsg,
icon: "none"
});
}
});
uni.request({
url: _url,
method: 'get',
responseType: 'arraybuffer',
header: {
Authorization: token,
},
success: res => {
uni.hideLoading();
if (res.statusCode == 200) {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
} else {
uni.showToast({
title: res.errMsg,
icon: "none"
});
}
},
fail: err => {
uni.hideLoading();
uni.showToast({
title: res.errMsg,
icon: "none"
});
}
})
}
downloadFile(url) {
const baseURL="...."
const token = uni.getStorageSync('token');
let fileUrl = baseURL + url;
uni.showLoading({
title: '加载中...'
});
uni.downloadFile({
url: fileUrl,
success: (res) => {
if (res.statusCode === 200) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: (red) => {
uni.hideLoading();
uni.showToast({
icon: 'none',
mask: true,
title: '文件已保存:' + red.savedFilePath,
duration: 3000
});
setTimeout(() => {
uni.openDocument({
filePath: red.savedFilePath,
success: (ress) => {},
fail(err) {}
});
}, 200);
}
});
}
}
});
},