0

I've got working email sender, but without attachments. I tried something like this:

Simple input in HTML:

<input type="file" simple-change="uploadFiles($event)"/>

JS file picker:

    $scope.uploadFiles = function (event) {
    $scope.file= event.target.files[0];
};

And JS email sender:

$scope.sendEmail = function(){
    $scope.emailData = new EmailData();
    $scope.emailData.to = "[email protected]";
    $scope.emailData.from = "[email protected]";
    $scope.emailData.subject = "Errors";
    $scope.emailData.title = $scope.topic;
    $scope.emailData.description = $scope.description;
    $scope.emailData.template = "templateErrors";

    $http.post("sendemail/attachment", $scope.emailData, $scope.file, {headers: {'Content-Type': undefined} })
    .then(function (response) {
        $scope.succes =  true;
    },
    function(fail) {
        $scope.error = true;
    });
}

Java Controller:

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestBody EmailData emailData, CommonsMultipartFile file) {
    return emailSenderAttachment.sendDataAttach(emailData, file);
}

but still got error 500: ""message":"org/apache/commons/fileupload/FileUploadException""

Please help :(

3
  • 2
    What is the full stacktrace of the exception ? Commented Dec 21, 2017 at 15:37
  • Umm im not sure.. ure talkking about full error ({"timestamp":"2017-12-21T15:49:20.888+0000","status":500,"error":"Internal Server Error","exception":"java.lang.NoClassDefFoundError","message":"org/apache/commons/fileupload/FileUploadException","path":"/sendemail/zalacznik"}) or just about full of Java code? Commented Dec 21, 2017 at 15:49
  • Umm i added dependency, which I missed to add CommonsMultipartFile. But who know why I've got null in file object (java side)? Code same as above Commented Dec 22, 2017 at 11:46

1 Answer 1

1

Try below code use $upload.upload instead of $http.post for upload file to server.

AngularJs

$upload.upload({
    method : 'POST',
    url : 'sendemail/attachment',
    file : $scope.file,
    data : $scope.emailData
}).success(function(response) {
    console.log("Success");
}).error(function(error) {
    console.log("Error");
});

Spring Controller

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestParam("file") MultipartFile file, EmailData emailData) {
    return emailSenderAttachment.sendDataAttach(emailData, file);
}

For more details you can refer my answer from this post Multipart File Upload using AngularJS and SpringMVC

Sign up to request clarification or add additional context in comments.

3 Comments

I've got still error like: "message":"Required request part 'file' is not present"
Try this @RequestMapping(value = "/attachment", method = RequestMethod.POST, headers = ("content-type=multipart/*")) add headers.
glad to hear this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.