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

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

View File

@ -113,7 +113,7 @@ export const fetchAndDownload = async (url, data, method='post') => {
method, method,
url, url,
data, data,
responseType: 'blob',
headers: { headers: {
'X-Sign': sign, 'X-Sign': sign,
'X-TIMESTAMP': signMd5Utils.getTimestamp(), 'X-TIMESTAMP': signMd5Utils.getTimestamp(),
@ -128,19 +128,18 @@ export const fetchAndDownload = async (url, data, method='post') => {
const response = await Axios(config) const response = await Axios(config)
const { status, headers, data: responseData } = response const { status, headers, data: responseData } = response
if (status == 200) { if (status == 200) {
if (typeof responseData == 'object') { if (responseData.type == 'application/json') {
const { message: msg } = responseData const res = await readFile(responseData)
const parsed = JSON.parse(res)
const { message: msg } = parsed
message.error(msg) message.error(msg)
throw new Error(msg) throw new Error(msg)
} else { } else {
const disposition = headers['content-disposition'] const disposition = headers['content-disposition']
const fileName = getFileNameByHeaderContentDisposition(disposition) const fileName = getFileNameByHeaderContentDisposition(disposition)
if (typeof responseData == 'string') { saveAs(responseData, fileName)
const blob = new Blob([responseData], { type: headers['content-type'] })
saveAs(blob, fileName)
return fileName return fileName
} }
}
} else { } else {
message.error('This operation fails. Contact your system administrator') message.error('This operation fails. Contact your system administrator')
throw new Error('This operation fails. Contact your system administrator') throw new Error('This operation fails. Contact your system administrator')