[英]Can primeng p-fileupload component handle return type out-of-the-box?
I'm using primeng p-fileupload
component to upload a single file.我正在使用primeng
p-fileupload
组件上传单个文件。 Back-end API is Spring Boot
that uploads file to hdfs
and returns the path.后端 API 是
Spring Boot
上传文件到hdfs
并返回路径的引导。 The file uploads fine but the onUpload()
angular function is not called.文件上传正常,但未
onUpload()
angular function。 I know this is due to the returned path variable as with a void
return type it works.我知道这是由于返回的路径变量,因为它使用
void
返回类型。 How can I get the path back with the default p-fileupload
component (without having to write a custom upload function)?如何使用默认的
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);
}
}
Figured this out.想通了。
My Spring Boot
API was returning a String
instead of JSON.我的
Spring Boot
API 返回一个String
而不是 JSON。 Correct code returns ResponseEntity
with a response object that has one variable called path
(this is serialized to JSON under the hood).正确的代码返回带有响应 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));
}
In Angular
can get returned JSON with event.originalEvent.body.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.