【问题标题】:axios POST using blob as responseTypeaxios POST 使用 blob 作为 responseType
【发布时间】:2021-09-17 05:01:42
【问题描述】:

所以我正在尝试通过 axios 和 blob 下载文件。问题是我需要传递密码,因此我不能使用“GET”请求。出于测试目的,我仍然发出“GET”请求让我开始。由于项目即将结束,我想最终解决这个问题,但我找不到解决方案。

这是我的工作代码,带有“GET”请求,可以给你一个想法。

                    axios({
                        url: `/api/download/?uuid=${uuid}&password=${password}`,
                        method: "GET",
                        responseType: "blob",
                    })
                    .then((response) => {

                        var fileURL = window.URL.createObjectURL(
                            new Blob([response.data])
                        );
                        var fileLink = document.createElement("a");

                        fileLink.href = fileURL;
                        fileLink.setAttribute("download", filename);
                        document.body.appendChild(fileLink);

                        fileLink.click();
                        self.showLottie = false;
                    })
                    
                    .catch(function (error) {

                        self.showLottie = false;

                        alert("Download failed");

                    });

【问题讨论】:

  • 能否提供后台功能?
  • @HenriqueVanKlaveren 给你

标签: rest post download axios blob


【解决方案1】:

好的,基本上我所要做的就是将 uuid 和密码放入 params 对象中。

  axios({
                        url: `/api/download`,
                        method: "GET",
                        responseType: "blob",
                        params: {
                                uuid,
                                password,
                                },
                    })
                    .then((response) => {

                        var fileURL = window.URL.createObjectURL(
                            new Blob([response.data])
                        );
                        var fileLink = document.createElement("a");

                        fileLink.href = fileURL;
                        fileLink.setAttribute("download", filename);
                        document.body.appendChild(fileLink);

                        fileLink.click();
                        self.showLottie = false;
                    })
                    
                    .catch(function () {

                        self.showLottie = false;

                        alert("Download failed");

                    });

解释:Are HTTPS URLs encrypted?

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2020-06-12
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2013-05-26
    • 2020-07-06
    相关资源
    最近更新 更多