0

I need to send file via POST in my web application. I have a server side in java and a client side in angular 2. I need to client send file to the server. Server's code:

@RequestMapping(method = RequestMethod.POST, value = "/run/file")
@ResponseBody
private void runImportRecordsJob(@RequestParam("file") MultipartFile file){
    // Some code
}

Client's code:

Component:

export class ImportRecordsJobComponent implements OnInit {

  file: File;


  constructor(private jobsService: JobsService) { }


  chooseFile(event: any){
    this.file = event.srcElement.files[0];

    console.log(this.file);
  }

  selectFormat(event: any){
    if (event.length > 0)
      this.format = event[0].key;
    else
      this.format = null;

  }

  runImportRecordsJob(){
    if (confirm("Are you sure you want to run this job?")){
      this.jobsService.runImportRecordsJob({file: this.file});
    }
  }

  ngOnInit() {
  }

}

Service:

@Injectable()
export class JobsService {

  constructor(private http: Http) { }

  runImportRecordsJob(importRecords: any){
    var headers = new Headers({"Content-Type": 'application/json; multipart/form-data;'});
    let options = new RequestOptions({ headers: headers });

    let formData = new FormData();
    formData.append("file", importRecords.file, importRecords.file.name);

    this.http.post(SERVER + "/batches/run/file", formData, options).subscribe();
  }

}

But I'm getting an error: nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request

and formData is always empty. Can anyone suggest me a way how to send file without using ng2-uploader and stuff like that. Thanks.

1 Answer 1

1

Did you miss the "CommonsMultipartResolver" bean?

<bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 20 * 1024 * 1024 Byte = 20 MB-->
        <property name="maxUploadSize" value="20971520" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

Edit:

Changed method on the service:

  runImportRecordsJob(importRecords: any){
    let options = new RequestOptions();

    let formData = new FormData();
    formData.append("file", importRecords.file, importRecords.file.name);

    console.log(formData);
    console.log(importRecords);
    this.http.post(SERVER + "/batches/run/file", formData, options).subscribe();
  }
Sign up to request clarification or add additional context in comments.

3 Comments

No, I've configured the bean: @Bean public CommonsMultipartResolver multipartResolver(){ CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setDefaultEncoding("utf-8"); commonsMultipartResolver.setMaxUploadSize(50000000); return commonsMultipartResolver; }
var headers = new Headers({"Content-Type": 'application/json; multipart/form-data;'});
Can you remove the application header?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.