Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. uploader.bind('FilesAdded', function(up, files) {
  2. //uploader.start();
  3.  
  4. //First of all, we are going to add more info to the files
  5. for(var i = 0; i < files.length; i++) {
  6. var file = files[i];
  7. var inputFile = file.getNative();
  8.  
  9. file.lastModified = inputFile.lastModified;
  10. file.lastModifiedDate = inputFile.lastModifiedDate;
  11. file.type = inputFile.type;
  12. file.image_thumb = undefined;
  13. file.response = undefined;
  14.  
  15. //If is an image
  16. if(file.type && file.type.split('/')[0].indexOf('image') > -1) {
  17.  
  18. //While we are loading just set up a default mime
  19. file.image_thumb = mimeService.getUrl(file);
  20.  
  21. var fr = new FileReader();
  22. /* jshint -W083 */
  23. fr.onload = (function(file) {
  24. return function(evt) {
  25. //Retrieve an image, set up a canvas and crop the image
  26. var image = new Image();
  27. image.src = evt.target.result;
  28. image.onload = function() {
  29.  
  30. var newWidth = image.width;
  31. var newHeight = image.height;
  32.  
  33. if(newWidth > newHeight) {
  34. if(newWidth > MAX_IMAGE_WIDTH) {
  35. newHeight = Math.ceil(newHeight * MAX_IMAGE_WIDTH / newWidth);
  36. newWidth = MAX_IMAGE_WIDTH;
  37. }
  38. } else {
  39. if(newHeight > MAX_IMAGE_HEIGHT) {
  40. newWidth = Math.ceil(newWidth * MAX_IMAGE_HEIGHT / newHeight);
  41. newHeight = MAX_IMAGE_HEIGHT;
  42. }
  43. }
  44.  
  45.  
  46. var canvas = document.createElement('canvas');
  47. canvas.width = newWidth;
  48. canvas.height = newHeight;
  49. var ctx = canvas.getContext('2d');
  50. ctx.drawImage(image, 0, 0, newWidth, newHeight);
  51.  
  52. scope.$apply(function() {
  53. file.image_thumb = canvas.toDataURL(file.type);
  54. });
  55. };
  56. };
  57.  
  58. })(file);
  59. /* jshint +W083 */
  60.  
  61. fr.readAsDataURL(inputFile);
  62. } else {
  63. //Here we set up one image that will represent the file
  64. file.image_thumb = mimeService.getUrl(file);
  65. }
  66. }
  67.  
  68. scope.$apply(function() {
  69. if(iAttrs.plFilesModel) {
  70. angular.forEach(files, function(file, key) {
  71. scope.plFilesModel.push(file);
  72. });
  73. }
  74.  
  75. if(iAttrs.onFileAdded) {
  76. scope.$parent.$eval(iAttrs.onFileAdded, {$files: files});
  77. }
  78. });
  79.  
  80. if(iAttrs.plAutoUpload == 'true') {
  81. uploader.start();
  82. }
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement