A simple file sharing site with an easy to use API and online panel.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

53 lines
2.1KB

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