![](/img/trans.png)
[英]Is there a way to set the PrimeNG p-fileUpload component to open in a custom folder?
[英]Can primeng p-fileupload component handle return type out-of-the-box?
我正在使用primeng p-fileupload
组件上传单个文件。 后端 API 是Spring Boot
上传文件到hdfs
并返回路径的引导。 文件上传正常,但未onUpload()
angular function。 我知道这是由于返回的路径变量,因为它使用void
返回类型。 如何使用默认的p-fileupload
组件获取路径(无需编写自定义上传功能)?
<p-fileUpload name="file" url="http://<MY-IP>:8080/upload" accept=".csv" maxFileSize="1000000" (onUpload)="onUpload($event)"></p-fileUpload>
onUpload(event) {
for (let file of event.files) {
console.log(file);
this.uploadedFiles.push(file);
console.log(this.uploadedFiles);
}
}
想通了。
我的Spring Boot
API 返回一个String
而不是 JSON。 正确的代码返回带有响应 object 的ResponseEntity
,该响应具有一个名为path
的变量(这在引擎盖下被序列化为 JSON)。
@PostMapping("/upload")
public ResponseEntity upload(@RequestParam("file") MultipartFile file) {
final String path = fileUploadService.uploadFile(file, fileStorageProperties.getUploadDir());
return ResponseEntity.status(HttpStatus.CREATED).body(new UploadFileResponse(path));
}
在Angular
中,可以使用 event.originalEvent.body.path 返回event.originalEvent.body.path
。
onUpload(event) {
for (let file of event.files) {
let path: string = event.originalEvent.body.path;
console.log(path);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.