Kondziowsky
Kondziowsky

Reputation: 173

Ng file upload with another data

I want to add my uploaded file by ng-file-upload to another data which i sending to my Java backend. I wondering how to do it, when I have to put url in my .upload. In this case that can't work cause sendMail sending firstly file, next the text data. How can I fix it?

$scope.sendMail = function(){   

$scope.uploadFiles = function(file) {
    $scope.attach = file;
    file.upload = Upload.upload({
        data: {file: file} 
    });
}

$scope.emailData = new EmailData();
$scope.emailData.to = "[email protected]";       
$scope.emailData.from = "[email protected]";
$scope.emailData.type = "TYPE";
$scope.emailData.title = $scope.data.title;
$scope.emailData.descr = $scope.data.description;
$scope.emailData.template = "template";
$scope.emailData.file = $scope.attach;

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

Upvotes: 0

Views: 648

Answers (2)

Ramin Farajpour
Ramin Farajpour

Reputation: 417

this is example from ng-file-upload Github

$scope.upload = function (file) {
    Upload.upload({
        url: 'upload/url',
        data: {file: file, 'username': $scope.username}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
};

as you see you can send what you want inside data

Upvotes: 1

johnjerrico
johnjerrico

Reputation: 143

use content-type: multipart form/data & make use form-data; have a look at this link https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

Upvotes: 1

Related Questions