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 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 apiBaseUrl = window._CONFIG['domianURL'] || '/jeecg-boot'
const sign = signMd5Utils.getSign(url, data) const sign = signMd5Utils.getSign(url, data)
@ -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(),
@ -121,26 +121,25 @@ export const fetchAndDownload = async (url, data, method='post') => {
'tenant-id': Vue.ls.get(TENANT_ID) 'tenant-id': Vue.ls.get(TENANT_ID)
} }
} }
if(method == 'get') { if (method == 'get') {
config.params = data config.params = data
} }
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')