如何解决Angular和Node js中的zip文件上传问题
我正在尝试上载包含许多json文件的zip文件,我需要在服务器端读取zip文件并读取其中的内容(json文件)。但是在尝试上载zip文件时,我收到了错误的请求,已经尝试了所有可能的解决方案示例followed this stackoverflow answer,但是由于我没有在angular中进行任何文件上传,并且nodejs需要一些指导,因此我遇到了错误的请求错误。在我编写的用于上传zip文件的代码下方,并创建了一个api端点来查看上传的内容。
HTML
<div class="form-group">
<label for="file">Choose File</label>
<input type="file" id="file" #file(change)="importData(element.name,element.id,$event.target.files)">
</div>
TS文件代码
importData(name,id,files){
let fileList: FileList = files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadFile',file,file.name);
formData.append('fileType','zip');
let headers = new Headers();
headers.append('Accept','application/json');
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:4400/data' + '/importConfiguration/newProd12345/1.0',formData,{headers: {"Accept": "application/json",enctype:"multipart/form-data"}})
// .map(res => res.json())
// .catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),error => console.log(error)
)
}}
我在选择文件和进行上传请求时遇到错误
用于生成ZIP文件的代码
router.get("/exportConfiguration/:productId/:productVersion",async (req,res) => {var productId=req.params.productId;var productVersion=req.params.productVersion;
var formulas= await getFormulas(productId,productVersion);
var rules = await getRules(productId,productVersion)
res.writeHead(200,{
'Content-Type': 'application/zip','Content-disposition': 'attachment; filename=myFile.zip'
});
var zip = Archiver('zip');
// Send the file to the page output.
zip.pipe(res);
// Create zip with some files. Two dynamic,one static. Put #2 in a sub folder.
zip.append(JSON.stringify(formulas),{ name: 'formulas.json' })
.append(JSON.stringify(rules),{ name: 'rules.json' })
// .append('Some text to go in file 2. I go in a folder!',{ name: 'somefolder/2.txt' })
// .file('staticFiles/3.txt',{ name: '3.txt' })
.finalize();});
对于如何解决此问题的想法的任何帮助,将不胜感激。预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。