I want to upload a file in angular. I have tried following :
<div >
<form enctype="multipart/form-data" method="post" novalidate name="form">
<input type="file" onchange="angular.element(this).scope().setFile(this)" required>
{{files.name}}
<div >
<button type="button" ng-class="{'tbt-btn':true, 'primary-btn':true}" ng-click="uploadFile(form,files)">Save</button>
</div>
</form>
</div>
JS---
$scope.uploadFile = function (form,files) {
if (form.$invalid) return;
var path = files;
var uploadProgress = function (e) {
if (e.lengthComputable) {
var percent = Math.round(e.loaded * 100 / e.total);
console.log('upload progress: ' + percent + '%');
}
},
uploadComplete = function (e) {
if (e.target.status !== 200) {
uploadError(e);
return;
}
var locations = JSON.parse(e.target.responseText);
//function I want to call
},
uploadError = function (e) {
},
uploadAbort = function (e) {
};
var fd = new FormData();
var files = files;
for (var i = 0; i < files.length; i++) {
fd.append("file", files[i]);
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadError, false);
xhr.addEventListener("abort", uploadAbort, false);
xhr.open("POST", "/fileupload");
xhr.send(fd);
}
but its not working .I am getting error - "Missing initial multi part boundary" Also , I am not sure what parameters I need to pass in xhr.open();. Can you please help me on this?
Thanks