7

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

2 Answers 2

23

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. For others I can add that if you already have the Content-Type header being set by Angular (my issue), you can do headers: myHeaders.delete('Content-Type')
4

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);
    }
}

2 Comments

What Content-Type is Angular 6 setting for you? In my case it's application/json and I don't think that's right...
The content-type in my case is multipart/form-data when not specified in header : Content-Type: multipart/form-data; boundary=----WebKitFormBoundarytKcUBulaQJNwYb6P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.