Ren*_*gas 2 file-upload typescript primeng angular
我在我的 html 中声明 FileUpload 如下
<p-fileUpload name="file" url="http://localhost:8080/integra/services/upload" (onUpload)="onUpload($event)" accept=".txt"></p-fileUpload>
Run Code Online (Sandbox Code Playgroud)
它指向我的后端
@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);
}
Run Code Online (Sandbox Code Playgroud)
但是当在我的打字稿中执行 onload 事件时,它不起作用......甚至放置了一个 console.log 来测试,但它不起作用。这是我的打字稿
onUpload(event) {
console.log(event.files.length);
console.log(event.progress);
}
Run Code Online (Sandbox Code Playgroud)
当我运行文件上传时,加载栏被卡住,不允许我执行另一个文件上传。因此,当加载栏从未完成时,不会执行提到的事件 在此处输入图像描述
最后,文件保存在上述路径中,但我无法正确处理该事件
如果有人能帮助我,我将不胜感激。谢谢
小智 6
我在“onUpload”方法中遇到了同样的问题(对我来说不起作用),你可以使用uploadHandlerandcustomUpload="true"相反,它就像一个魅力!
这是一些示例代码:
超文本标记语言
```
<p-fileUpload #fileInput
name="files"
(uploadHandler)="onUpload($event)"
customUpload="true"
></p-fileUpload>
```
Run Code Online (Sandbox Code Playgroud)
TS
onUpload(event) {
console.log('onUpload', event);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3390 次 |
| 最近记录: |