0

I want to extract the file through HTML and AngularJS to Java Spring with Hibernate. So, for example I've got in my HTML just:

<button type="button" class="btn btn-success btn-lg" ng-click="sendMail()">
<span class="glyphicon glyphicon glyphicon-send"></span> Wyślij
</button>
<input type="file" file-upload multiple name="file"/>   
<button ng-click="add()">Add</button>

And i guess that's all for HTML.

JS file:

$scope.sendMail = function(){  
 $scope.emailData = new EmailData();
 $scope.emailData.to = "konrad@gmail.com";       
 $scope.emailData.from = "emergency@gmail.com";
 $scope.emailData.subject = "Sending maila";
 $scope.emailData.type = "Sending failure";
 $scope.emailData.title = $scope.data.title;
 $scope.emailData.descriptMail = $scope.data.description;
 $scope.emailData.description = "Chrome 5.4.0";

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

}

So, how can I get this file which i drop and click add? I wanted, just like with other datas take $scope.emailData.attachement = ""; to use this in my Spring file.

Example:

in js I've got $scope.emailData.description = "Chrome 5.4.0"; and I can get it by spring: private String description; and stuff with getters and setters

and then I can use it.

I'm not good in Spring and backend (yet), so that's why I am asking you

Kondziowsky
  • 173
  • 1
  • 14
  • Are you attempting to send an email or upload a file? – wholevinski Aug 25 '17 at 11:33
  • I am attempting to send my email but with a file from user (look at html). I can send mail with no problem, but I want to add this one file from user. – Kondziowsky Aug 25 '17 at 11:37
  • 1
    Do you have any code sending to the server yet? If not, you'll probably want to familiarize yourself with the $http service: https://docs.angularjs.org/api/ng/service/$http#post – wholevinski Aug 25 '17 at 11:49
  • 1
    And here's a SO answer showing how to post form data (your email properties like to, from, subject, etc) and the file: https://stackoverflow.com/a/25264008/769971 – wholevinski Aug 25 '17 at 11:51
  • Thank you, this will be great for me to understand this. Yes, I've got a query for sending my email. For example i've got ng-model="data.description" in my textarea and then i've got scope in JS and send it to java. Question is, can i for example do the same thing with file or not? – Kondziowsky Aug 25 '17 at 11:55
  • 1
    In the SO post I linked, they're using a FormData object. FormData allows you to append a file as well as regular form data: https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects – wholevinski Aug 25 '17 at 11:59
  • 1
    I would recommend adding your angular code that talks to your Java side to your original post since that's the important part with respect to your problem. – wholevinski Aug 25 '17 at 12:01
  • Okay I paste a code of js – Kondziowsky Aug 25 '17 at 12:52

0 Answers0