[英]How to receive zip file with nodejs
好吧,問題來了。
我正在嘗試將 ZIP 文件從 Python Flask 服務器發送到 NodeJS 后端,以便我可以從前端網頁下載它。 不幸的是,當請求的響應到達后端時,它會觸發“HPE_INVALID_CONSTANT”代碼錯誤。
這是 Python 代碼
zipfolder = zipfile.ZipFile(filename,"w",compression=zipfile.ZIP_STORED)
zipfolder.write(file)
zipfolder.write(file2)
zipfolder.close()
return sendfile(filename,mimetype="zip",download_name=zfile,as_attachment="True")
這是頁面后端的代碼:
async function data(vars){
return new Promise(function(resolve,reject){
form = new FormData()
//insert data in form
axios.post(url,form,{headers:form.getHeaders(),maxBodyLength:Infinity}).then((resp)=>{
var zip = new JSZip()
zip.loadAsync(resp.data,{base64:true}).then((z)=>{
resolve(z)
})
}).catch(err=>{
reject(err)
})
})
}
使用此配置,我收到錯誤Error: Invalid base64 input, bad content length.
如果我將{base64:true}
替換為{base64:false}
,我會收到錯誤Error: Corrupted zip: missing 45305 bytes.
關於我做錯了什么的任何想法?
您可能缺少responseType: 'arraybuffer'
,因此 Axios 正在嘗試將您的 zip 數據解碼為字符串並在運行時破壞它。 除此之外,您將變量( form
等)泄漏到全局,並且您在濫用承諾,所以總而言之,事情可能會簡化為:
async function data(vars) {
const url = '...';
const form = new FormData();
// ... put things in the form...
const resp = await axios.post(url, form, {
headers: form.getHeaders(),
responseType: 'arraybuffer',
});
const zip = new JSZip();
return zip.loadAsync(resp.data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.