![](/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.