【问题标题】: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"})
  1. 作为响应,我得到的是损坏的数据,而不是字节。
  2. 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

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2017-07-27
      • 2019-11-21
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多