HttpErrorResponse 200 after successfull POST in Angular

Viewed 12758

I upload one file from Angular 5.xx to Jersey 1.xx via FormData.

The data gets received and saved successfully on my server app directory, but Browser says this line from the picture (Chrome and Firefox).

When I upload it via HTML only like so:

  Choose file to upload<br>
    <form action="http://localhost:8181/BackendMaven" method="post" enctype="multipart/form-data">
        <input name="input" id="filename" type="file" /><br><br>
        <button name="submit" type="submit">Upload</button>
    </form>

it won't show up.

Here is the angular FromData Code:

@ViewChild('fileInput') fileInput; 
submitFile(): void{ 
    console.log("submitFIle called!!!");
    let fi = this.fileInput.nativeElement;
     let fileToUpload = fi.files[0];
     let formData = new FormData();//empty formdata
     formData.append("input", fileToUpload);
     console.log(formData.get("input"));
     this.http.post(this.URI_UPLOAD,formData).subscribe();}

And Server Side (im using tomcat)

public Response uploadFile(@FormDataParam("input") InputStream uploadedInputStream, @FormDataParam("input") FormDataContentDisposition fileDetail) {
  ...
        return Response.status(200).entity("Successfully uploaded to location: " + FileFactory.getFilePath(uploadedFileLocation)).build();

    }

} 

enter image description here

2 Answers

i have the same error

you must change the response from json to text

datatype by default is JSON

this way make new error in observable and the solution of this case

this.http.post(url, body, { responseType: 'text' }).subscribe();

this way make new error because observable and the solution in this case

ERROR in src/app/myS.service.ts(24,54): error TS2322: Type '"text"' is not assignable to type '"json"'

you must use this pattern to solution it

    returnObservable(): Observable<any> {
  const requestOptions: Object = {
    /* other options here */
    responseType: 'text'
  }
  return this.http.get<any>(url, body, requestOptions);
}
Related