我的应用程序有问题。我在 Angular 5 上上传了一个文件,但我无法提供给我的 Java Spring 后端以将其插入数据库。
当我尝试将frmData
发送到 Java 控制器时收到 500 错误。
有人有解决方案吗?这是我的代码:
我的组件.html:
<ng-container>
<input type="file" id="uploadFile" multiple
(change)="getFileDetails($event.target.file)">
</ng-container></div>
我的组件 .ts :
getFileDetails (e) {
//console.log (e.target.files);
for (var i = 0; i < e.target.files.length; i++) {
this.myFiles.push(e.target.files[i]);
}
}
addFollowUp1() {
const frmData = new FormData();
for (var i = 0; i < this.myFiles.length; i++) {
frmData.append("file", this.myFiles[i]);
}
this.followUpService.addFollowUpAttachment(frmData).subscribe();
}
和我的服务:
public addFollowUpAttachment(file: FormData): Observable<any> {
return this.http.post('/SV-AUD/api/attachment/addAttachment', file );
}
在java后端这里是控制器:
@Controller
@RequestMapping("/attachment")
public class AttachmentController {
@Autowired
private AttachmentService attachmentService;
@RequestMapping(value = "/addAttachment", method = RequestMethod.POST)
@ResponseBody
public void addFollowUpAttachment(@RequestParam("file") MultipartFile files) throws ApcException {
System.out.println("test");
attachmentService.addFollowUpAttachment(files);
}
}
谢谢!
我发现了问题,而不是$event.target.file,我只用了$event,现在我终于从java后端xD得到一个错误
<ng-container>
<input type="file" id="uploadFile" multiple (change)="getFileDetails($event)">
</ng-container></div>