用 Axios 上传文件的几种方式
post("some/api/url", someDataForBackend, {
responseType: "blob",
})
.then(async (response) => {
const isJsonBlob = (data) => data instanceof Blob && data.type === "application/json";
const responseData = isJsonBlob(response?.data) ? await (response?.data)?.text() : response?.data || {};
const responseJson = (typeof responseData === "string") ? JSON.parse(responseData) : responseData;
console.log(responseJson)
});
axios.get(`/download/blob/api`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
validateStatus: (s) => s <= 500,
responseType: 'blob',
}).then(async (res) => {
if (res.status !== 200) {
// error handling
const error = JSON.parse(await res.data.text());
console.log('error: ', error);
} else {
// success blob file
}
});