A simple file sharing site with an easy to use API and online panel.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
2.1KB

  1. angular.module('UploadComp', ['ngFileUpload', 'AuthSvc']).component('uploadComponent', {
  2. templateUrl: '/views/shimapan/upload-form.html',
  3. controller: ['$scope', 'Upload', '$timeout', 'AuthService', function ($scope, Upload, $timeout, AuthService) {
  4. $scope.errToString = function (err) {
  5. if (err === 'maxSize')
  6. return "File too large.";
  7. else
  8. return err;
  9. };
  10. $scope.uploadFiles = function (files, errorFiles) {
  11. $scope.files = $scope.files ? $scope.files.concat(files) : files;
  12. $scope.errorFiles = $scope.errorFiles ? $scope.errorFiles.concat(errorFiles) : errorFiles;
  13. angular.forEach(files, function (file) {
  14. file.upload = Upload.upload({
  15. url: '/api/upload',
  16. method: 'POST',
  17. file: file
  18. });
  19. file.upload.then(
  20. function (response) {
  21. $timeout(function () {
  22. file.result = response.data;
  23. });
  24. },
  25. function (response) {
  26. if (response.status !== 200) {
  27. if (response.status === 401) {
  28. file.$error = "Invalid authorization token";
  29. } else if (response.status === 403) {
  30. file.$error = "Forbidden";
  31. } else {
  32. file.$error = "Unknown error " + response.status;
  33. }
  34. var index = $scope.files.indexOf(file);
  35. $scope.errorFiles.push(file);
  36. $scope.files.splice(index, 1);
  37. }
  38. },
  39. function (evt) {
  40. file.progress = Math.floor(Math.min(100.0, 100 * evt.loaded / evt.total));
  41. }
  42. );
  43. });
  44. };
  45. }],
  46. controllerAs: 'vm'
  47. });