Created
September 14, 2018 04:06
-
-
Save jquery404/734054667ea92fac3b8b577324775050 to your computer and use it in GitHub Desktop.
File upload service for angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<form ng-submit="SubmitAssignment()" enctype="multipart/form-data"> | |
<div class="form-group"> | |
<label class="control-label">Upload File</label> | |
<input type="file" file-model="myFile" /> | |
</div> | |
<button class="btn btn-info" type="submit">Submit</button> | |
</form> | |
<script> | |
// services | |
app.service('fileUploadService', function ($http, $q) { | |
this.uploadFileToUrl = function (file, uploadUrl) { | |
//FormData, object of key/value pair for form fields and values | |
var fileFormData = new FormData(); | |
fileFormData.append('file', file); | |
var deffered = $q.defer(); | |
$http.post(uploadUrl, fileFormData, { | |
transformRequest: angular.identity, | |
headers: {'Content-Type': undefined} | |
}).success(function (response) { | |
deffered.resolve(response); | |
}).error(function (response) { | |
deffered.reject(response); | |
}); | |
return deffered.promise; | |
} | |
}); | |
app.controller('assignmentDetailsCtrl', function ($scope, $http, fileUploadService) { | |
$scope.SubmitAssignment = function () { | |
// upload attachment file before submit | |
var file = this.myFile; | |
if (file) { | |
var uploadUrl = "php/assignmentUploadService.php", | |
promise = fileUploadService.uploadFileToUrl(file, uploadUrl); | |
promise.then(function (response) { | |
$scope.serverResponse = response; | |
if (response.status == 0) | |
$scope.pushAssignment(response, true); | |
}, function () { | |
$scope.serverResponse = 'An error has occurred'; | |
}); | |
} else { | |
$scope.pushAssignment(null, false); | |
} | |
}; | |
$scope.pushAssignment = function (cid, attachment) { | |
// submit assignment | |
var content = this.assignmentContent; | |
var reqdata; | |
if (attachment) { | |
reqdata = { | |
task: 1, | |
st_id: $scope.assignmentInfo.st_id, | |
student_id: student_id, | |
content: content, | |
assignment_id: $scope.assignmentInfo.assignment_id, | |
ext: cid.ext, | |
url: '', | |
attachment: 'yes', | |
attachment_ext: cid.ext, | |
attachment_filename: cid.filename, | |
attachment_on: 'submission', | |
} | |
} else { | |
reqdata = { | |
task: 1, | |
st_id: $scope.assignmentInfo.st_id, | |
student_id: '<?php echo $session_student['student_id']; ?>', | |
content: content, | |
assignment_id: $scope.assignmentInfo.assignment_id, | |
ext: '', | |
url: '', | |
attachment: 'no' | |
} | |
} | |
$http({ | |
method: 'POST', | |
url: 'ws/ws_assignment_post.php', | |
data: $.param(reqdata), | |
headers: {'Content-Type': 'application/x-www-form-urlencoded'} | |
}).then(function (response) { | |
var data = response.data; | |
if (data.status === 0) { | |
window.location.href = "<?php echo nex()->lms_student_url() ?>/assignments.php"; | |
} | |
}); | |
}; | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment