我在构建此角度的上传文件代码时遇到麻烦。 编辑:在邮递员中,我成功上传了一个文件。仅角度代码失败。 快速路由器代码为(uploadRouter):
uploadRouter.route('/')
.post(upload.single('txtFileFilter'), (req,res)=>{
res.statusCode = 200;
res.setHeader('Content-Type','application/json');
res.json(req.file);
path = req.file.path;
console.log('upload completed for '+path);
})
app.use('/api/txtupload', uploadRouter)
角度服务代码为:
pushFileToStorage(file: File): Observable<HttpEvent<{}>> {
const formdata: FormData = new FormData();
formdata.append('file', file);
const req = new HttpRequest('POST', 'http://localhost:8080/api/txtupload', formdata, {
reportProgress: true,
responseType: 'text'
});
return this.http.request(req);
}
组件代码为:
upload() {
this.progress.percentage = 0;
this.currentFileUpload = this.selectedFiles.item(0);
this.uploadService.pushFileToStorage(this.currentFileUpload).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.progress.percentage = Math.round(100 * event.loaded / event.total);
} else if (event instanceof HttpResponse) {
console.log('File is completely uploaded!');
}
});
this.selectedFiles = undefined;
}
我测试时收到POST http://localhost:8080/api/txtupload 500(内部服务器错误)和ERROR HttpErrorResponse {标题:HttpHeaders,状态:500,statusText:“内部服务器错误”,url:“ http://localhost:8080/api/txtupload”,好的:false,…}