【问题标题】:p-fileUpload : how to read file after uploadp-fileUpload : 上传后如何读取文件
【发布时间】:2018-10-24 09:49:46
【问题描述】:

我尝试从 prime-ng 库中读取使用以下组件上传的文件。

我的 html 组件:

<h3 class="first">Upload an excel file</h3>
<p-fileUpload name="myfiles[]"
              chooseLabel="Choose a file"
              uploadLabel="Show data
              cancelLabel="Cancel"
              accept=".csv"
              maxFileSize="1000000"
              auto="auto"
              customUpload="true"
              (uploadHandler)="myUploader($event)">
  <ng-template pTemplate="toolbar">
    <p-button label="Show data" icon="fa fa-upload" iconPos="left" (onClick)="myUploaderEnd($event)"></p-button>
  </ng-template>
  <ng-template pTemplate="content">

  </ng-template>
</p-fileUpload>

<p *ngIf="datas.length > 1">{{datas[2].reference}}</p>
<p>{{debug}}</p>

我的 TypeScript 组件:

import { Component, OnInit } from '@angular/core';
import {Message} from "primeng/api";
import {Data} from "../../shared/data";
import {DataService} from "../../services/data.service";
import {Routes, RouterModule, Router} from '@angular/router';

@Component({
  selector: '....',
  templateUrl: './file-upload-data.component.html',
  styleUrls: ['./file-upload-data.component.css']
})
export class FileComponent implements OnInit {
  dataService: DataService = new DataService();
  uploadedFiles: any[] = [];
  fileBuffer:string = "";
  fileLines:string[] = [];
  datas:Datas[] = new Array<Datas>();
  debug: string = "";
  file: File;

  constructor(private router: Router) { }

  ngOnInit() {
  }


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

  myUploaderEnd(event) {
    this.readFile(this.uploadedFiles[0]);
    //this.datas= this.dataService.populateDatas(this.fileLines);
    this.debug = this.uploadedFiles[0].name+"----"+this.fileBuffer;
  }

  readFile(file: File) {

    var reader = new FileReader();
    reader.onload = () => {
      this.fileBuffer+=reader.result;
    };
    reader.readAsText(file);
    this.fileLines = this.fileBuffer.split("\n");
  }
  
}

我第一次点击调用函数的按钮:myUploaderEnd 我没有任何数据,fileBuffer 的值为空。 我第二次单击同一个按钮时,我在变量中有值...

我想在第一次点击时从文件中读取数据,但我没有解释为什么我需要点击两次。

谢谢。

【问题讨论】:

    标签: angular typescript primeng


    【解决方案1】:

    在调试期间,reader.onload 事件是代码的最后一部分,称为 so ...

      export class FileComponent {
      uploadedFiles: any[] = [];
      fileBuffer:string = "";
      fileLines:string[] = [];
      debug: string = "";
      file: File;
      reader: FileReader;
    
      constructor(private router: Router,private importService: ImportService ) {
        this.importService.setActiveIndex(0);
        this.reader = new FileReader();
        this.reader.onload = () => {
          this.fileBuffer=this.reader.result;
          this.fileLines = this.fileBuffer.split("\n");
          this.debug += ""+this.uploadedFiles[0].name+"----"+this.fileBuffer+"!!!!!!";
          this.importService.setActiveIndex(1);
          this.router.navigate(["/app/import/consulter"]);
        };
    
      }
    
      myUploader(event) {
        for(let file of event.files) {
          this.uploadedFiles.push(file);
        }
      }
    
      myUploaderEnd(event) {
        this.readFile(this.uploadedFiles[0]);
      }
    
      readFile(file: File) {
        this.reader.readAsText(file);
      }
    

    【讨论】:

      【解决方案2】:

      这就是我在 p-fileupload 中读取文件的方式:

      html 文件:

      <p-fileUpload 
          [files]="externalFiles" 
          #uploader 
          [multiple]="false" 
          accept="image/*" 
          maxFileSize="1000000" 
          [showUploadButton]="false"
          [showCancelButton]="false" 
          (onSelect)="onSelectImage($event.files)" 
          (onRemove)="onRemoveImage($event)">
      </p-fileUpload>
      

      TS 文件:

      public onSelectImage(evt: any) {
         this.uploadedFile = evt[0];
      }
      

      你应该可以从evt[0]获取上传的文件

      【讨论】:

      • 感谢您的帮助,我找到了解决办法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多