CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Upload Multipart File In Angular

Upload Multipart File In Angular

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Paribartan Kalathoki
By Paribartan Kalathoki
Published: March 28, 2022 ยท 2 min read ยท 0 Comments
Share: in X
upload multipart file in angular

In this short post, we will learn to upload a multipart file using angular.

1. Create an input field In your file.component.ts file

<div class="form-group">
    <label for="file">Choose File</label>
    <input type="file" id="file" class="file-input" accept=".png, .jpg, .jpeg" (change)="onFileSelected($event.target.files)" />
</div>

2. Upload handling in typescript

Define global variable. For example:-

fileToUpload: File | null = null;

Create a method that will be triggered when the user selects a file.

onFileSelected(event: FileList) {
    this.fileToUpload = event.item(0);
}

Now prepare to set the file in the multipart format and send in the API URL request to save the file.

onSaveFile() {
   const formData: FormData = new FormData();
   formData.append('file', this.fileToUpload);
   
   return this.httpClient.post(YOUR_API_URL, formData);
}

Note:- If you want to convert the uploaded image into Base64 encoded format then you can use:

getBase64(file: Blob) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    });
}

On File upload, you can pass your file into the above method as a parameter, and will return the encoded value in base64 format in which you can set your variable and send as per the required format during the API call.

this.file = await this.getBase64(YOUR_FILE).then();

Remember to use await because the method you are using must be async.

3. Conclusion

In this post, we learned to upload the Multipart File in Angular Project. If you want to integrate it with REST API then you can take a reference to multipart file upload Spring Boot.

You can find the complete example code in the Github repository.

If you have any queries please feel free to ask.

Thank you ๐Ÿ™‚

Related Posts:

  • Event Handling in Java Swing
  • java.lang.Short Class in Java
  • How to Write User Stories and Acceptance Criteria: A…
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • Array Operator in MongoDB
  • Variable in Java
Tags:angularangular-file-uploadfile-uploadmultipart-file-upload
Was this article helpful?
โ† Previous ArticleMultipart File Upload Spring Boot
Next Article โ†’Eclipse Code Formatter Code Template for Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap