简体   繁体   English

p-fileUpload:上传后如何读取文件

[英]p-fileUpload : how to read file after upload

I try to read a file uploaded with the component below from the prime-ng library.我尝试从 prime-ng 库中读取使用以下组件上传的文件。

My html component:我的 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>

My TypeScript component:我的 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");
  }
  
}

The first time i click on the button which call the the function: myUploaderEnd i dont have any data, the value of fileBuffer is null. The second time i click on the same button i have value in the variable...我第一次点击调用 function 的按钮:myUploaderEnd 我没有任何数据,fileBuffer 的值为 null。我第二次点击同一个按钮时我在变量中有值......

I would like to read data from file at the first click and i dont explain why i need two clicks.我想在第一次点击时从文件中读取数据,我不解释为什么我需要两次点击。

Thank you.谢谢你。

During debugging the reader.onload event is the last part of code called so...在调试期间,reader.onload 事件是代码的最后一部分,因此...

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

This is how I'm reading the file in p-fileupload:这就是我在 p-fileupload 中读取文件的方式:

html file: 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 file: TS文件:

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

You should be able to get the uploaded file from evt[0]您应该能够从evt[0]获取上传的文件

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

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