Return to site
· web applications,Web Development,Angular,file upload
broken image

Let us discuss how to do file upload in Angular.js in this session.

Create an HTML template with input tag of input type as file. Create a function to event for handling choosing files.

    Choose File

    

           id="file"

           (change)="handleFileInput($event.target.files)">

Now set a default variable for selected file.

Create  a function to use in event of your file input tag.

    this.fileToUpload = files.item(0);

}

If you want to use multiple file selection then repeat through files array. The file upload function ie. file -upload-service:

    const endpoint = 'your-destination-url';

    const formData: FormData = new FormData();

    formData.append('fileKey', fileToUpload, fileToUpload.name);

    return this.httpClient

      .post(endpoint, formData, { headers: yourHeadersConfig })

      .map(() => { return true; })

      .catch((e) => this.handleError(e));