Skip to content

Instantly share code, notes, and snippets.

@alpocr
Created June 24, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpocr/617665643ad995f90700 to your computer and use it in GitHub Desktop.
Save alpocr/617665643ad995f90700 to your computer and use it in GitHub Desktop.
'use strict';
/**
* @ngdoc function
* @name mall4gApp.controller:AdminMediaUploadCtrl
* @description
* # AdminMediaUploadCtrl
* Controller of the mall4gApp
*/
angular.module('mall4gApp')
.controller('AdminMediaUploadCtrl', ['$scope', '$http', '$timeout', '$compile', '$upload', 'GAuth', 'GApi', 'adminData', 'adminService', '$rootScope', function ($scope, $http, $timeout, $compile, $upload, GAuth, GApi, adminData, adminService, $rootScope) {
$scope.admin = adminData.items[0];
$scope.user = $rootScope.gapi.user;
$scope.usingFlash = false;
$scope.fileReaderSupported = window.FileReader != null && (window.FileAPI == null || FileAPI.html5 != false);
$scope.$watch('files', function(files) {
$scope.formUpload = false;
if (files != null) {
for (var i = 0; i < files.length; i++) {
$scope.errorMsg = null;
(function(file) {
generateThumbAndUpload(file);
})(files[i]);
}
}
});
$scope.uploadPic = function(files) {
$scope.formUpload = true;
if (files != null) {
generateThumbAndUpload(files[0])
}
};
function generateThumbAndUpload(file) {
$scope.errorMsg = null;
$scope.generateThumb(file);
uploadUsing$upload(file);
}
$scope.generateThumb = function(file) {
if (file != null) {
if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
$timeout(function() {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
$timeout(function() {
file.dataUrl = e.target.result;
});
}
});
}
}
};
function uploadUsing$upload(file) {
// get url for upload
adminService.getUrlFileUpload().then(function(resp){
var urlToUpload = resp.url;
file.upload = $upload.upload({
url: urlToUpload,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
file: file,
fileFormDataName: 'myFile',
});
file.upload.then(function(response) {
$timeout(function() {
file.result = response.data;
console.log('subido', response.data);
});
}, function(response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
});
file.upload.progress(function(evt) {
// Math.min is to fix IE which reports 200% sometimes
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
file.upload.xhr(function(xhr) {
// xhr.upload.addEventListener('abort', function(){console.log('abort complete')}, false);
});
})
}
angular.element(window).bind('dragover', function(e) {
e.preventDefault();
});
angular.element(window).bind('drop', function(e) {
e.preventDefault();
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment