将响应转换为数组缓冲区
Converting response to arraybuffer
我正在尝试使用 axios 将响应转换为 arraybuffer。我不断收到一条错误消息:“TypeError:response.arrayBuffer 不是函数”。以下是我编写的代码以及对我做错了什么的建议或解决此错误的方法。
axios
.post("http://127.0.0.1:8000/api/file-api", formData, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Access-Control-Allow-Origin':'http://localhost:8080',
'Access-Control-Allow-Credentials': 'true',
'Authorization': `bearer ${token}`
},
})
.then((response) => response.arrayBuffer())
.then((res) => {
const arrayBuffer = res.arrayBuffer();
const objUrl = window.URL.createObjectURL(new Blob([arrayBuffer]));
return objUrl;
})
.catch((err) => {
console.log(err);
});
阅读这篇文章后 post [Axios 与 fetch():哪个最适合发出 HTTP 请求?][1],
[1]: https://blog.logrocket.com/axios-vs-fetch-best-http-requests/#:~:text=Axios%20automatically%20transforms%20the%20data,info%20on%20what%20the%20response。然后是@Bravo 的评论,
我注意到使用 axios 可能无法提供 arraybuffer 作为 responseType。
如果是arraybuffer,最好使用fetch。
我正在尝试使用 axios 将响应转换为 arraybuffer。我不断收到一条错误消息:“TypeError:response.arrayBuffer 不是函数”。以下是我编写的代码以及对我做错了什么的建议或解决此错误的方法。
axios
.post("http://127.0.0.1:8000/api/file-api", formData, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Access-Control-Allow-Origin':'http://localhost:8080',
'Access-Control-Allow-Credentials': 'true',
'Authorization': `bearer ${token}`
},
})
.then((response) => response.arrayBuffer())
.then((res) => {
const arrayBuffer = res.arrayBuffer();
const objUrl = window.URL.createObjectURL(new Blob([arrayBuffer]));
return objUrl;
})
.catch((err) => {
console.log(err);
});
阅读这篇文章后 post [Axios 与 fetch():哪个最适合发出 HTTP 请求?][1],
[1]: https://blog.logrocket.com/axios-vs-fetch-best-http-requests/#:~:text=Axios%20automatically%20transforms%20the%20data,info%20on%20what%20the%20response。然后是@Bravo 的评论, 我注意到使用 axios 可能无法提供 arraybuffer 作为 responseType。 如果是arraybuffer,最好使用fetch。