rest - axios POST 使用 blob 作为 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 是否加密?
推荐阅读
- python - 如何从网站获取文本段落:错误 403 Forbidden
- c# - 轮播图片未加载
- flutter - 渲染 Text() 时在 Widget 中 Flutter If 语句
- arrays - Laravel 从对象中的对象访问值
- scala - 我可以用什么来更好地编写这个 scala 代码?
- android - 如何使用导航组件获取上一个目的地?
- sed - 命令行参数与 .sed 文件输入的差异
- javascript - 使用 JavaScript 在 SortableJS 可排序列表上模拟拖放
- c++ - Qt,将参数传递给函数
- ios - 如何在 GFlops 中估计 OpenGL ES 着色器/金属性能