Dorin
Dorin

Reputation: 2266

Upload file using ng-file-upload and Spring Boot

I am trying to make a video upload using angular and spring boot. I am using ng-file-upload in angular and in the controller I am using the following method to upload the file

   Upload.upload({
       url: 'api/uploadFile',
       data: {file: file, 'username': $scope.username}
   })

On the server I have declared my resource like this

@RestController
@RequestMapping("/api")
public class FileUploadResource {

    @ResponseStatus(HttpStatus.OK)
    @PostMapping("/uploadFile")
    public void uploadFile(@RequestParam("file") MultipartFile file,  @RequestParam("username") String name)

When I try uploading the file I keep receiving a 500 error and I can't figure out how to solve it. If you could help me or if you could suggest another method that would be great

Thanks.

Upvotes: 1

Views: 855

Answers (2)

Reza
Reza

Reputation: 543

Set headers in your upload to:

headers: { 'Content-Type': undefined }

It's better to send your data like this:

var formData = new FormData();
formData.append("file", file);
formData.append("username", username);

And put formData as upload's data to send.

Upvotes: 0

httPants
httPants

Reputation: 2123

Is the uploadFile controller method being called? I'm guessing an exception is being thrown from the uploadFile method, in which case you should have a stack trace showing in your server log that will direct you to where the problem lies.

Upvotes: 1

Related Questions