【问题标题】:How to download files using axios.post in React using webapi Core如何使用 webapi Core 在 React 中使用 axios.post 下载文件
【发布时间】:2019-09-17 23:14:13
【问题描述】:
我已使用此How to download files using axios.post from webapi(可能与此重复)来处理这种情况,但我没有成功。
React:
axios.post("URL",{data:data, responseType: "blob"})
- 作为响应,我得到的是损坏的数据,而不是字节。
- ResponseType 适用于 get 请求,但为什么不适用于 post?
WebAPI Core:
return File(arraybytes, "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet", "test.xlsx");
【问题讨论】:
标签:
reactjs
axios
asp.net-core-webapi
【解决方案1】:
以下是我能够下载托管在 S3 AWS 存储桶上的视频的方法,
const handleDownload = () => {
const link = document.createElement("a");
link.target = "_blank";
link.download = "YOUR_FILE_NAME"
axios
.get(url, {
responseType: "blob",
})
.then((res) => {
link.href = URL.createObjectURL(
new Blob([res.data], { type: "video/mp4" })
);
link.click();
});
};
我在一个带有 onClick 的按钮中触发了 handleDownload 功能。
函数中的 url 包含来自 S3 存储桶的视频 URL