node.js - 角度和节点js中的zip文件上传问题
问题描述
我正在尝试上传一个包含许多 json 文件的 zip 文件,我需要在服务器端读取 zip 文件并读取它的内容(json 文件)。但是我在尝试上传 zip 文件时收到了错误的请求已经尝试了所有可能的解决方案示例,遵循了这个 stackoverflow 答案,但是我收到了错误的请求错误,因为我没有在 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();});
非常感谢任何有关如何解决此问题的想法帮助。提前致谢。
解决方案
推荐阅读
- c# - MimeKit 附件正文编码问题
- delphi - 显示模态时调用覆盖的 ShowModal 时出错
- android - 如何在浮动操作模式下在 Android 9 上显示菜单项图标
- c++ - 错误:没有重载函数“mbed::Ticker::attach”的实例与参数列表匹配
- compiler-errors - 在 Modelsim VHDL 中编译
- python - Python - 从熊猫系列中删除停用词的功能
- reactjs - 无法读取未定义反应复选框过滤器的属性“包含”
- php - 在表单提交时增加 PHP 变量
- python - 修复积分器
- swift - UITableView 标题中的自定义视图重叠 UIViewController 视图