完善功能
This commit is contained in:
@@ -11,11 +11,10 @@ const router = useRouter();
|
||||
|
||||
// 请求地址前缀
|
||||
let baseURL = ''
|
||||
console.log(process.env.NODE_ENV)
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// 生产环境
|
||||
baseURL = "http://120.26.251.180:8085/api/"
|
||||
// baseURL = "http://192.168.0.100:8085/api/"
|
||||
// baseURL = "http://192.168.0.108:8085/api/"
|
||||
} else {
|
||||
// 开发环境
|
||||
baseURL = ""
|
||||
@@ -85,7 +84,6 @@ export function postAxios({ url, data }) {
|
||||
}
|
||||
}
|
||||
).then(res => {
|
||||
console.log(res)
|
||||
}).catch(err => {
|
||||
if (err.message == "Network Error") {
|
||||
// alert("网络错误,请检查网络连接")
|
||||
@@ -128,5 +126,38 @@ export function postAxios({ url, data }) {
|
||||
})
|
||||
})
|
||||
}
|
||||
export const downFile = async (urlstr, data) => {
|
||||
|
||||
// 发送请求,获取文件流
|
||||
const response = await axios.post(urlstr, data, { responseType: 'blob' });
|
||||
|
||||
// 获取文件名(如果后端设置了 Content-Disposition)
|
||||
const contentDisposition = response.headers['content-disposition'];
|
||||
let fileName = 'default-file-name'; // 默认文件名
|
||||
console.log(contentDisposition)
|
||||
console.log(response)
|
||||
if (contentDisposition) {
|
||||
// 从响应头中提取文件名
|
||||
const fileNameMatch = contentDisposition.match(/filename="(.+)"/);
|
||||
if (fileNameMatch && fileNameMatch.length > 1) {
|
||||
fileName = fileNameMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
// 创建一个临时的下载链接
|
||||
const blob = new Blob([response.data], { type: response.headers['content-type'] });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName; // 设置下载的文件名
|
||||
a.click();
|
||||
|
||||
// 释放 URL 对象
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default axios
|
||||
Reference in New Issue
Block a user