简体   繁体   English

axios POST 使用 blob 作为 responseType

[英]axios POST using blob as responseType

So I am trying to download a file via axios and blob.所以我试图通过 axios 和 blob 下载文件。 The problem is I need to pass a password, and for that reason I can't use a "GET" request.问题是我需要传递密码,因此我不能使用“GET”请求。 For test purposes I still made a "GET" request to get me started.出于测试目的,我仍然提出了一个“GET”请求来让我开始。 Since the project comes to an end I wanted to finally fix that issue but I can't find a solution.由于项目即将结束,我想最终解决该问题,但找不到解决方案。

That is my working code with a "GET" request to give you an idea.这是我的工作代码,带有“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");

                    });

Ok, so basically all I had to do was putting the uuid and password in a params object.好的,所以基本上我所要做的就是将 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");

                    });

Explanation: Are HTTPS URLs encrypted?说明: HTTPS URL 是否加密?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM