How to send multipart form-data for a file upload in Angular

0 votes
Can i know How to send multipart/form-data for a file upload in Angular?
Feb 24, 2025 in Angular by Nidhi
• 16,260 points
738 views

1 answer to this question.

0 votes

In Angular, you can send files using FormData with HttpClient to make a multipart/form-data request.

Step 1: Create File Upload Service

import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders, HttpEvent, HttpRequest } from '@angular/common/http';

import { Observable } from 'rxjs';

@Injectable({

  providedIn: 'root'

})

export class FileUploadService {

  private apiUrl = 'https://your-api-endpoint.com/upload'; // Replace with actual API

  constructor(private http: HttpClient) {}

  uploadFile(file: File): Observable<HttpEvent<any>> {

    const formData = new FormData();

    formData.append('file', file);

    const req = new HttpRequest('POST', this.apiUrl, formData, {

      reportProgress: true, // Enables progress tracking

      responseType: 'json'

    });

    return this.http.request(req);

  }

}

Step 2: Create File Upload Component

import { Component } from '@angular/core';

import { FileUploadService } from './file-upload.service';

@Component({

  selector: 'app-file-upload',

  template: `

    <input type="file" (change)="onFileSelected($event)">

    <button (click)="onUpload()" [disabled]="!selectedFile">Upload</button>

  `

})

export class FileUploadComponent {

  selectedFile: File | null = null;

  constructor(private fileUploadService: FileUploadService) {}

  onFileSelected(event: Event) {

    const input = event.target as HTMLInputElement;

    if (input.files?.length) {

      this.selectedFile = input.files[0];

    }

  }

  onUpload() {

    if (this.selectedFile) {

      this.fileUploadService.uploadFile(this.selectedFile).subscribe(event => {

        console.log('Upload Event:', event);

      });

    }

  }

}

answered Feb 24, 2025 by Navya

Related Questions In Angular

0 votes
1 answer

How to pass data from a child component to a parent component in Angular 4?

In Angular 4, passing data from a ...READ MORE

answered Dec 4, 2024 in Angular by Navya
708 views
0 votes
2 answers

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,800 points
30,688 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,800 points
1,863 views
0 votes
1 answer

How do I include a JavaScript script file in Angular and call a function from that script?

Hello @kartik, Refer the scripts inside the angular-cli.json (angular.json when using ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,800 points
15,010 views
0 votes
1 answer

Should I use map or switchmap when using angular http module?

When working with the Angular HTTP module, ...READ MORE

answered Feb 24, 2025 in Angular by Navya
533 views
0 votes
1 answer

How does BehaviorSubject differ from Subject in state management?

Feature Subject BehaviorSubject Initial Value No initial value Requires an initial value Last ...READ MORE

answered Feb 24, 2025 in Node-js by Navya
551 views
0 votes
1 answer

How can you implement forkJoin for multiple API responses aggregation?

In Angular, forkJoin is used to combine ...READ MORE

answered Feb 24, 2025 in Node-js by Navya
620 views
0 votes
1 answer

What is the use of takeUntil to cancel a subscription?

takeUntil is an RxJS operator used to ...READ MORE

answered Feb 24, 2025 in Node-js by Navya
655 views
0 votes
1 answer

How to set a default base URL for all API calls in Angular?

In Angular, you can set a default ...READ MORE

answered Feb 26, 2025 in Angular by Kavya
1,255 views
0 votes
1 answer

How to transfer data between two unrelated components in Angular?

Steps to Transfer Data Between Unrelated Components 1. ...READ MORE

answered Dec 12, 2024 in Angular by Navya
829 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP