如何在nodejs中保存文件



我在前端使用angular,代码如下:在app.component.html 中

<input type="file" (onchange)="handlefile($event)">

在app.component.ts 中

file: File | null = null
handlefile(event:any): void{
this.file = event.target.files.item(0);
}
uploadfile(){
this.uploadservice.postFile(this.file);
}

在上传service.service.ts:

postFile(file: File|null): void{
if(file!=null){
let headers = new HttpHeaders({
'Content-Type':'application/json'
});
var json = JSON.stringfy({file: file});
this.http.post('http://localhost:3000/mpost',json, {headers: headers});
}
}

在后端:

app('/mpost', (req, res) => {
console.log(req.body.file)
});

在nodejs控制台中,我得到了这个{}。我正在询问如何保存此文件或将其添加到猫鼬模式中。

尝试使用Express中间件,如Express fileuploadnpm Express fileupload。以下是完整的教程:如何在Node.js和Express中上传文件

最新更新