0

I am using angular 6.I am sending image file data through form data.But I am enable to get file data in API.here is my code

 UpdateLogoprofile(data: any): Observable<any> {
    debugger;
    const model = {};
    const url = AppSettings.API_ENDPOINT + 'Client/UploadFileToStorage';
    console.log(data);
   // const body = JSON.stringify(data);
   //const body = data;
    const httpOptions = {
        headers: new HttpHeaders({
        'content-type': 'application/json',
        'Authorization': 'bearer ' + this.auth_token + ''
           })
         };
    return this.http.post(url, data, httpOptions)
        // .map(this.extractData)
        .map((response: Response) => <any>response.json())
        .catch(this.handleError);
    }

and FormData

 let formData: FormData = new FormData();
                     formData.append(file.name,file);
                    // formData.append('StudentId',this.StudentID);
                    debugger;
                    this._ClientService.UpdateLogoprofile(data).subscribe(
                        image => {

The Problem: I am not able to acces formData in API

Ling Vu
  • 4,740
  • 5
  • 24
  • 45
Anusha
  • 31
  • 1
  • 9
  • Are you seeing your image data in the request that is being sent to your API ? –  Oct 23 '18 at 13:29
  • You might want to refer to this one : https://stackoverflow.com/questions/47936183/angular-file-upload – Karthik Oct 23 '18 at 13:32
  • Problem is not able to acces formdata in API – Anusha Oct 23 '18 at 13:55
  • I changed and checked but no lucky – Anusha Oct 23 '18 at 14:38
  • Content type should be `'content-type': 'multipart/form-data'` as when your form includes any element you should use multipart/form-data and change `this._ClientService.UpdateLogoprofile(data)` to `this._ClientService.UpdateLogoprofile(formData)` – Suryan Oct 23 '18 at 19:10

2 Answers2

0

Change the file append line with

formData.append('key', file);
sunera pathan
  • 357
  • 1
  • 3
  • 9
0

Please correct this line

this._ClientService.UpdateLogoprofile(data)

to

this._ClientService.UpdateLogoprofile(formData);

Also in header content type should be 'content-type': 'multipart/form-data'

Pinki Dhakad
  • 150
  • 5