我正在尝试上载包含许多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();});
对于如何解决此问题的想法的任何帮助,将不胜感激。预先感谢。