【问题标题】:The method onUpload is not running in p-fileUpload of PrimengPrimeng的p-fileUpload中没有运行onUpload方法
【发布时间】:2020-02-07 07:17:06
【问题描述】:

我在我的 html 中声明一个 FileUpload,如下所示

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

指向我的后端

    @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);
    }

但是在我的打字稿中执行我的 onload 事件时,它不起作用......甚至一个 console.log 被放置来测试但它不起作用。 这是我的打字稿

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

当我运行文件上传时,加载栏卡住了,不允许我执行另一个文件上传。因此,当加载栏从未完成时,不会执行提到的事件 enter image description here

最后,文件保存在上述路径中,但我无法正确处理该事件

如果有人可以帮助我,我将不胜感激。 谢谢

【问题讨论】:

  • 文件是否到达后端?您可以尝试 (uploadHandler)="onUpload($event)" 自己处理上传。 maybe this can help

标签: angular typescript file-upload primeng


【解决方案1】:

我在使用“onUpload”方法时遇到了同样的问题(对我不起作用),您可以使用 uploadHandlercustomUpload="true" 代替,它就像一个魅力!

这里是一些示例代码:

HTML

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

TS

  onUpload(event) {
    console.log('onUpload', event); 
  }

【讨论】:

  • 如果可能,请努力提供额外的解释,而不仅仅是代码。此类答案往往更有用,因为它们可以帮助社区成员,尤其是新开发人员更好地理解解决方案的推理,并有助于避免需要解决后续问题。
猜你喜欢
  • 2017-03-23
  • 2020-08-29
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
相关资源
最近更新 更多