Home  >  Forum  >  Angular
Post New Query

Passing null while sending file from angular4 to springboot

Asked on June 05, 2018

Iam uploading file using spring boot and angular4,I have to save that uploaded file in my local machine and filename in database.
but while sending file from frontend to backend its passing null.

Its showing me 500 error like this while passing from frontend(angular4) to backend(springboot)---

      body: "{\"errorMessage\":null,\"requestedURI\":\"/core-location/importFiles\"}"

This is my code-

   <td><span id="starmark">*</span>UploadTemplate:</td>
     <input type="file" (change)="fileChange($event)"> 
     <input type="submit" value="submit">

fileChange(event) {
    let fileList: FileList = event.target.files;
    console.log("file event==>",fileList);
    if (fileList.length > 0) {
      let file: File = fileList[0];
      let formData: FormData = new FormData();
      formData.append('uploadFile', file, file.name);
      let headers = new Headers();
     headers.append('Accept', 'application/json');
     headers.append('enctype', 'multipart/form-data');
      let options = new RequestOptions({ headers: headers });
      this.http.post(importData,formData, options)
        .map(res => res.json())
        .catch(error => Observable.throw(error))
        data => console.log('success'),
        error => console.log(error)
  upload(form: NgForm) {
    console.log("inside upload");
    .subscribe(data => {

 pushFileToStorage(importData:Import): Observable<Import> {

    let cpHeaders = new Headers();
    cpHeaders.append('Content-Type', 'application/json');
    cpHeaders.append('Accept', `application/json`);
    cpHeaders.append('X-XSRF-TOKEN', this._cookieService.get("XSRF-TOKEN"))
    let options = new RequestOptions({ headers: cpHeaders});
    const formdata: FormData = new FormData();
    formdata.append('file', 'importData');
    return this.http.post(importFiles,importData,options)
        .map(success => success.status)

----------Springboot controller--------------

public class Controller {
    private static String UPLOADED_FOLDER = "E://temp//";

 RequestMapping(value ="/importFiles", method = RequestMethod.POST)
 public String singleFileUpload(@RequestBody Import importData, MultipartFile file) {
    System.out.println("inside files");
     try {
        // Get the file and save it somewhere
        byte[] bytes = file.getBytes();
        Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
        Files.write(path, bytes);
    } catch (IOException e) {

File is coming to backend from frontend but its not entering to try block and its not saving into E drive.

Can anyone help me to resolve.

Thanks and regards
shakti Lohar

Replied on June 05, 2018
What is the error log in backend? Is e.printStackTrace() printing any message?

Replied on June 05, 2018
@Mohit  Thanks for the reply, No exception but file is passing null.

---in error log it is printing like this---

error log:

inside files
impt com.example.location.domain.Import@7cdf0fae
file null
file 222E://temp//

can you please check.?

Replied on June 05, 2018
MultipartFile needs @RequestParam for eg.

public String singleFileUpload(@RequestBody Import importData, @RequestParam("file") MultipartFile file) {


Replied on June 06, 2018
@Mohit  Thanks for the reply,
If i use @RequestParam("file")  Its giving me 500 error by saying that, Current request is not a multipart request.

In Responce it is printing like this,

{"errorMessage":"Current request is not a multipart request","requestedURI":"/location/importFiles"}

Write Answer

©2019 concretepage.com | Privacy Policy | Contact Us