简体   繁体   English

p-fileUpload 不能多次启动

[英]p-fileUpload does not work more than once primeng

I`ll try to explain what it is going on with this component, I have defined the component this way我将尝试解释这个组件发生了什么,我已经这样定义了组件

<p-fileUpload #fileUpload accept=".csv,.txt" maxFileSize="1000000" customUpload="true" (uploadHandler)="uploadFile($event)">

on my package Json I have this "primeng": "^4.1.0-rc.3" and have this method on the js file在我的包 Json 上,我有这个“primeng”:“^4.1.0-rc.3”,并且在 js 文件中有这个方法

 uploadFile(event) {

    for (let file of event.files) {
        this.uploadedFiles.push(file);
    }

    this.uploadFileService.doSomething(this.uploadedFiles[0]).subscribe(x => {
        this.fileInfo = x;
        ..do some stuff..

    });
}

It works as expected the first time (showing 3 buttons of the component choose, upload and cancel) and letting me choose the file from the popup window and the process continues calling the uploadFile method;它第一次按预期工作(显示组件选择、上传和取消的 3 个按钮)并让我从弹出窗口中选择文件,然后该过程继续调用 uploadFile 方法; the thing is, once it finishes, if I try to upload again the same file it does not allow me to do it.问题是,一旦它完成,如果我尝试再次上传同一个文件,它不允许我这样做。 It only allows me to choose the file, but the upload button is never enabled and, of course, the uploadFile method is never called.它只允许我选择文件,但从不启用上传按钮,当然,从不调用 uploadFile 方法。 But if I choose another file different from the previous, it works as expected, that means calling the uploadFile method.但是如果我选择另一个不同于以前的文件,它会按预期工作,这意味着调用 uploadFile 方法。

I am using我正在使用

this.fileUpload.clear();

just to clear the part where the uploaded file it is showed to the user只是为了清除向用户显示上传文件的部分

Any suggestion?有什么建议吗?

I think so this issue might have been resolved, but for future reference of developers, please find the solution below.我想这个问题可能已经解决了,但为了供开发人员将来参考,请在下面找到解决方案。

Please refer for solution and explanation: [ https://github.com/primefaces/primeng/issues/4018][1]解决方法和解释请参考:[ https://github.com/primefaces/primeng/issues/4018][1]

Please pass the template variable to component and then use that variable to clear.请将模板变量传递给组件,然后使用该变量清除。

For example:例如:

HTML file: HTML文件:

<p-fileUpload #fileUpload accept=".csv,.txt" maxFileSize="1000000" customUpload="true" (uploadHandler)="uploadFile($event,fileUpload)">

COMPONENT file:组件文件:

uploadFile(event,fileUpload) {

    for (let file of event.files) {
        this.uploadedFiles.push(file);
    }

    this.uploadFileService.doSomething(this.uploadedFiles[0]).subscribe(x => {
        this.fileInfo = x;
        ..do some stuff..

    });

    fileUpload.clear(); // this will clear your file
}

This works.这有效。

Html :( Make a Reference ie, #fileUpload ) Html :(参考即 #fileUpload )

<p-fileUpload #fileUpload mode="basic" name="upload[]" maxFileSize="1000000" (onSelect)="onFileChange($event)" chooseLabel="Upload" auto="auto"></p-fileUpload>

TS: (Manipulate Using the Reference): TS:(使用参考进行操作):

 @ViewChild('fileUpload') fileUpload: any;

Invoke clear method when needed to clear the fileUpload and do the following:在需要清除 fileUpload 时调用 clear 方法并执行以下操作:

clear(){
      this.fileUpload.clear();
    }

and onFileChange($event) will work as usual like the below:和 onFileChange($event) 将像往常一样工作,如下所示:

 onFileChange(event) {
        for(let file of event.files) {
            this.customUploadedFiles.push(file);
      }
  } 

I see problem in using 'this.fileUpload.clear()'.我看到使用“this.fileUpload.clear()”的问题。 Replace 'this.fileUpload.clear()' with below code.用下面的代码替换“this.fileUpload.clear()”。

this.fileUpload.files.forEach((value, index) => {
 this.fileUpload.files.splice(index, 1);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM