zip file uploading issue in angular 2

前端 未结 1 1707
执念已碎
执念已碎 2021-01-17 01:56

when i try to upload zip file then unfortunately it doesn\'t get uploaded on the server. It gives me error something went!!! Here is my code:

const event = t         


        
相关标签:
1条回答
  • 2021-01-17 02:30

    you could use something like that:

    <input type="file" formControlName="uploadFile" (change)="uploadFileToServer($event)"/>
    
    
    import { Component } from '@angular/core';
    
    import { RequestOptions, Headers, Http } from '@angular/http';
    
    @Component({
    
    
    
     selector: 'file-uploader',
      templateUrl: './uploadFile.component.html',
      styleUrls: ['./uploadFile.component.css'],
    })
    export class FileUploadComponent {
    
    public uploadFileToServer(event) {
      let fileList: FileList = event.target.files;
      if (fileList.length > 0) {
        let file: File = fileList[0];
        let formData: FormData = new FormData();
        formData.append('uploadFile', file, file.name);
        formData.append('fileType', 'zip');
        let headers = new Headers();
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this.http.post('your url here', formData, options)
          .map(res => res.json())
          .catch(error => Observable.throw(error))
          .subscribe(
          data => console.log('success'),
          error => console.log(error)
          )
      }
    } 
    
    }
    

    0 讨论(0)
提交回复
热议问题