fix: 修复文件下载时乱码的问题

This commit is contained in:
Xu Zhimeng 2023-11-14 11:12:31 +08:00
parent fb23e422d8
commit e0ae878a6d

View File

@ -104,7 +104,7 @@ export function getFileNameByHeaderContentDisposition(contentDisposition) {
return fileName
}
export const fetchAndDownload = async (url, data, method='post') => {
export const fetchAndDownload = async (url, data, method = 'post') => {
const apiBaseUrl = window._CONFIG['domianURL'] || '/jeecg-boot'
const sign = signMd5Utils.getSign(url, data)
@ -113,7 +113,7 @@ export const fetchAndDownload = async (url, data, method='post') => {
method,
url,
data,
responseType: 'blob',
headers: {
'X-Sign': sign,
'X-TIMESTAMP': signMd5Utils.getTimestamp(),
@ -121,25 +121,24 @@ export const fetchAndDownload = async (url, data, method='post') => {
'tenant-id': Vue.ls.get(TENANT_ID)
}
}
if(method == 'get') {
if (method == 'get') {
config.params = data
}
const response = await Axios(config)
const { status, headers, data: responseData } = response
if (status == 200) {
if (typeof responseData == 'object') {
const { message: msg } = responseData
if (responseData.type == 'application/json') {
const res = await readFile(responseData)
const parsed = JSON.parse(res)
const { message: msg } = parsed
message.error(msg)
throw new Error(msg)
} else {
const disposition = headers['content-disposition']
const fileName = getFileNameByHeaderContentDisposition(disposition)
if (typeof responseData == 'string') {
const blob = new Blob([responseData], { type: headers['content-type'] })
saveAs(blob, fileName)
return fileName
}
saveAs(responseData, fileName)
return fileName
}
} else {
message.error('This operation fails. Contact your system administrator')