导出--处理后端传的文件流乱码问题
1.封装导出方法
export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', filename);
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank');
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
2.在请求接口添加参数
export function exportOutRecordByCon(data: any) {
return request({
responseType: 'blob',//接口返回文件流时添加该属性便不会乱码
headers: { 'Content-Type': 'multipart/form-data' },
url: '...',
method: 'GET',
params: data,
});
}```