如何使用axios下载包含多种文件类型的zip文件
在我的vueJs应用程序中,我有以下axios POST api调用,该调用应该返回希望保存在.zip文件夹中的多个文件.但是,当我使用下面的axios方法时,我无法打开我的zip文件,并出现一条错误消息,提示我的folder.zip无效".
In my vueJs application i have the following axios POST api call that is suppose to return multiple files that i wish to save on a .zip folder. however when i use the following axios method i can't open my zip and an error occurs saying that "my folder.zip is invalid".
我如何正确下载response.data?
How do i properly download the response.data?
这是方法.我在原始呼叫正文中发送了一些必要的信息,并发送了一个需要上传的文件,这样我就可以获取需要下载的其他文件.
Here is the method. I send a few needed information in my raw call body and a file that is needed to be uploaded so i can get the other files that i need to download.
upload() {
console.log("uploading")
let formData = new FormData();
let gerarMenus = "on";
let zipName= "test";
let var1= "on";
let var2= "on";
let var3= "on";
let var4= "on";
this.files.forEach((f,x) => {
formData.append('file'+(x+1), f);
});
formData.append('zipName', zipName);
formData.append('var1', var1);
formData.append('var2', var2);
formData.append('var3', var3);
formData.append('var4', var4);
axios
.post("/myUrl/downloadFile", formData)
.then((response) => {
console.log(response.data)
const url = window.URL.createObjectURL(new Blob([response]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', zipName+ '.zip');
document.body.appendChild(link);
link.click()}
).catch(e => {
console.error(e);
});
我尝试了tony19的答案,但是我仍然遇到相同的错误.不知道是否有帮助,但是我想获取多个xml和sql文件.
I tried tony19's answer but i still get the same error. Not sure if it is any help but i am suppose to get multiple xml and sql files.
这是我的console.log()中的response.data的打印件:
here's a print of response.data in my console.log():
尝试在您的请求中为 blob
设置 responseType
选项:
Try to set a responseType
option to blob
in your request:
axios
.post("/myUrl/downloadFile", formData, {
responseType: 'blob'
})
并在Blob构造函数中使用 response.data
,就像已经提到的@ tony19.
And use response.data
in a Blob constructor as @tony19 already mentioned.