1.下载流
downLoadFile = (option) => {
let params = {
originalFileName: option.file.originalFileName,
relativePath: option.file.relativePath,
newFileName: option.file.newFileName
};
unCrudeHttp({
method: 'post',
url: `${window.API_HOST}/business/resourcefile/download`,
responseType: "blob",
data: params
}).then(res => {
let data = res.data;
const blob = data;
const fileName = `${option.file.originalFileName}`;
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
});
}
2.下载base64
downLoadFile = (option) => {
let params = {
originalFileName: option.originalFilename,
relativePath: option.sftpVO.directory,
newFileName: option.sftpVO.fileName
};
billingSystemApi.fileDownloadApi(params).then(res => {
let content = res.data
// 使用atob方法解码base64
var raw = window.atob(content);
// 创建一个存储解码后数据的数组
var uInt8Array = new Uint8Array(raw.length);
// blob只能接收二进制编码,需要讲base64转为二进制再塞进去
for (var i = 0; i < raw.length; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
// 这里给了一个返回值,在别的方法掉用传入base64编码就可以得到转化后的blob
const link = document.createElement('a')
const blob = new Blob([uInt8Array], { type: 'application/vnd.ms-excel' })
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
//设置下载的Excel表名
link.setAttribute('download', `${option.originalFilename}`) //要有文件名后缀,如.xslx
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
this.setState({loading: false})
}).catch(err=>{
this.setState({loading: false})
message.error('导出失败!')
})
}
base64返回结果:
{
“code”:“2000”,
“message”:“成功”,
“data”:“UEsDBBQACAgIuhtjO4BNFtJTGLL2NqWvf3MLltLChvsKCR9/wfSdj+HSb1RLp6jgXXTgqJo2fnYG3g+PazuQBXB6HDiSANhb…S54bWxdDIueG1sUEsFBgAAAAAKAAoAhQIAAO0PAAAAAA==”,
“traceId”:“b0bc2153abcefba6”
}