Home  >  Forum  >  Angular
Post New Query

Passing null while sending file from angular4 to springboot



Asked on June 05, 2018
Hello,

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-
 
------------Import.html----------

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


     
--------------Import.ts----------------
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))
        .subscribe(
        data => console.log('success'),
        error => console.log(error)
        )
      }
    }
  upload(form: NgForm) {
 
    console.log("inside upload");
    console.dir(form.value);
    this.uploadService.pushFileToStorage(form.value)
    .subscribe(data => {
  console.dir(data);
 
          });
     }
       
           
------------uploadService.ts----------------

 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');
    console.dir(importData);
    console.log("importData"+importData);
    return this.http.post(importFiles,importData,options)
  
        .map(success => success.status)
        .catch(this.handleError)

        }
       
       
----------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");
    System.out.println("impt"+importData);
    System.out.println("file"+file);
     try {
        // Get the file and save it somewhere
        byte[] bytes = file.getBytes();
        System.out.println("bytes"+bytes);
        Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
        System.out.println("path"+path);
        Files.write(path, bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }

   
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 [email protected]
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