簡體   English   中英

axios POST 使用 blob 作為 responseType

[英]axios POST using blob as responseType

所以我試圖通過 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");

                    });

好的,所以基本上我所要做的就是將 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");

                    });

說明: HTTPS URL 是否加密?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM