工具文件.js:
import saveAs from 'file-saver';
function dataURLtoBlob(b64Data, contentType) {
const bstr = atob(b64Data);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {
type: contentType,
});
}
function blobToFile(Blob, fileName) {
Blob.lastModifiedDate = new Date();
Blob.name = fileName;
return Blob;
}
export function downLoadFile(base64Data, fileName = "test") {
const file = blobToFile(dataURLtoBlob(base64Data, 'application/vnd.ms-excel;charset=utf-8'), fileName);
saveAs(file, fileName);
}
接口:
export async function ExportSerialNumberList(params) {
return request(Ip + '/api/pes/ExportSerialNumberList', {
method: 'POST',
body: params,
});
}
组件:
import { downLoadFile } from '../../../utils/utils';
import {
ExportSerialNumberList,
} from '../../../services/waiting';
class:
exportSerialNumberList = debounce(() => {
ExportSerialNumberList({}).then(res => {
if (res) {
downLoadFile(res.FileContent, res.FileName);
}
});
}, 300);
render() {
return (
<Button onClick={this.exportSerialNumberList} type="primary">
导出
</Button>
)
}
|