Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData upload in angular4 application

In my angular4 application I'm trying to upload the video to server. But no matter what i add to the content type it always results in error from the server. In angular 1 the same api is hit using { 'Content-Type': undefined }

i tried the same in angular but it dint work. The data and everything is correct. I have tried with content-type set as below

headers.append('Content-Type', 'multipart/form-data');
headers.append('Authorization', token);
headers.set('Accept', 'application/json');

and also as below

headers.append('Content-Type', undefined);

below is the http request method:

public uploadVideo(formData: any) {


        var Upload = this.baseUrl + this.ngAuth.getApiUrl('Upload');
        var authData = JSON.parse(this.localStorage.localStorageGet('token'));
        var token = 'Bearer ' + authData.token;
        var self = this;

        var headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Authorization', token);
        headers.set('Accept', 'application/json');

        return this.http.post(Upload , formData, { headers: headers, method: 'POST' })
            .map((res: Response) => res.json());

    }

Please guide! Thanks

like image 351
Shruti Nair Avatar asked Oct 24 '17 12:10

Shruti Nair


2 Answers

So after going through a lot of solution i stumbled upon this issue

It says do not add the Content-Type to the header. So from my request header i removed

headers.append('Content-Type', 'multipart/form-data');

Thanks!

like image 103
Shruti Nair Avatar answered Nov 14 '22 11:11

Shruti Nair


I'll give a reference with Angular 6 and Spring Boot.

Angular 6 :

onSubmit(){
const formdata: FormData = new FormData();
console.log(this.currentFileUpload)
formdata.append('uploadedFile', this.currentFileUpload);
formdata.append('description',"My Desc");
formdata.append('companyName',"ABC")
formdata.append('numberOfRecords',"2")
console.log(formdata.get('uploadedFile'))

this.asyncService.makeUploadRequest('file/upload',formdata).subscribe(response => {
  console.log(response)
  if(response.responseCode == '00'){
    console.log('Successfully uploaded')
  }
  else {
    console.log("error")
  }
 });
}


makeUploadRequest(method, body) : Observable<any> {
    const url = "http://localhost:8097" + "/" + method;

    const headers = new Headers();
    this.token="Bearer"+" "+localStorage.getItem('token'); 
    headers.append('Authorization', this.token);
    // No need to set 'content type'
    const options = new RequestOptions({headers: headers});
    return this.http.post(url, body, options).pipe(
        map((response : Response) => {
            var json = response.json();                
         return json; 
        })
    );
}

Spring Boot API :

@RestController
@RequestMapping(value = { "/file" })
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Response uploadFile(@RequestParam("uploadedFile") MultipartFile uploadedFileRef){
        System.out.println(uploadedFileRef);
    }
}
like image 4
Shahbaaz Khan Avatar answered Nov 14 '22 12:11

Shahbaaz Khan