saravanam cs
saravanam cs

Reputation: 421

How to upload PDF file to server using angular 6

I generate HTML content to export PDF file using JsPDF library. Now, i need how to pass exported PDF file to server using angular 6. Thanks in advance

Upvotes: 0

Views: 3260

Answers (1)

Pinki Dhakad
Pinki Dhakad

Reputation: 150

Here is my way to do it.

 //HTML code

 <input type="file" name="File Upload" id="txtFileUpload" accept=".csv" 
  (change)="changeListener($event)" />


//Component.ts
  You have to import StaffService here:

 changeListener($event: any) {
    this.data = $event.target.files;
    this.postFile(this.data);
}

    postFile(inputValue: any): void {
    this.file = inputValue[0];   
    this.staffService.uploadCSV(this.file).subscribe(response => {
       //Do your next redirection or operation here
}, error =>{});
}

//StaffService.ts

    public uploadCSV(file): Observable<any> {
    const formdata = new FormData();
    formdata.append('files', file);
    return this.http.post(environment.apiUrlIp + this.urls.uploadCSVUrl, formdata, {
        reportProgress: true,
        responseType: 'json'
    });
}

Let me know if you find any issue to integrate this code. Happy to help.

Upvotes: 1

Related Questions