2

I am declaring a FileUpload in my html as follows

<p-fileUpload  name="file"  url="http://localhost:8080/integra/services/upload" (onUpload)="onUpload($event)"  accept=".txt"></p-fileUpload>

which is pointing to my backend

    @PostMapping(value="/upload", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        File convertFile = new File("C:\\upload\\"+file.getOriginalFilename());
        convertFile.createNewFile();
        FileOutputStream fout = new FileOutputStream(convertFile);
        fout.write(file.getBytes());
        fout.close();
        return new ResponseEntity("OK", HttpStatus.OK);
    }

But when executing my onload event in my typescript, it doesn't work ... and even a console.log is placed to test but it doesn't work. this is my typescript

  onUpload(event) {
    console.log(event.files.length);
    console.log(event.progress);
  }

When I run the fileupload the loading bar is stuck and does not allow me to perform another file upload. Therefore, when the loading bar is never completed, the mentioned event is not executed enter image description here

Finally, the file is saved in the mentioned path but I cannot handle the event correctly

I would be grateful that someone could help me. Thank you

1
  • Does the file get to the backend? You can try (uploadHandler)="onUpload($event)" to handle the upload by yourself. maybe this can help
    – riorudo
    Oct 9, 2019 at 22:22

1 Answer 1

7

i was facing the same problem with 'onUpload' method (did not work for me) , You can use uploadHandler and customUpload="true" instead , it worked like a charm !

Here is some sample code:

HTML

```
<p-fileUpload #fileInput
  name="files" 
  (uploadHandler)="onUpload($event)"
  customUpload="true"
></p-fileUpload>
```

TS

  onUpload(event) {
    console.log('onUpload', event); 
  }
1
  • When possible, please make an effort to provide additional explanation instead of just code. Such answers tend to be more useful as they help members of the community and especially new developers better understand the reasoning of the solution, and can help prevent the need to address follow-up questions.
    – Rajan
    May 21, 2020 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.