Last active
May 3, 2016 19:07
-
-
Save rafaellucio/05d8e64b3c17006936f7893a421d02cc to your computer and use it in GitHub Desktop.
Upload Component AngularJs
This file contains hidden or 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
| /*=============Module================*/ | |
| import service from './upload.service'; | |
| import directive from './upload.directive'; | |
| export default angular.module('app.upload', ['app.feedback']) | |
| .service('uploadService', service) | |
| .directive('upload', directive) | |
| .name; | |
| /*=============Service===============*/ | |
| var log = debug('app:service:upload'); | |
| export default class UploadService { | |
| constructor(restService, $q) { | |
| 'ngInject'; | |
| this.rest = restService; | |
| this.$q = $q; | |
| } | |
| upload(files) { | |
| var defer = this.$q.defer(); | |
| var formData = new FormData(); | |
| if (_.isEmpty(files)) { | |
| defer.resolve(""); | |
| return defer.promise; | |
| } | |
| files.forEach(function(file) { | |
| formData.append('files', file); | |
| }); | |
| return this.rest.init().all('upload').withHttpConfig({ | |
| transformRequest: angular.identity | |
| }).customPOST(formData, null, null, { | |
| 'Content-Type': undefined | |
| }); | |
| } | |
| } | |
| /*=============Directive=============*/ | |
| var log = debug('app:directive:upload'); | |
| var $inject = {}; | |
| class Upload { | |
| constructor() { | |
| this.restrict = 'A'; | |
| } | |
| link(scope, element, attrs) { | |
| element.attr('accept', 'image/jpg,image/jpeg,image/png,image/gif,image/bmp,text/plain,application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/vnd.oasis.opendocument.spreadsheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword,application/vnd.oasis.opendocument.text'); | |
| element.bind('change', () => { | |
| var arrayFiles = []; | |
| for (var i = 0; i < element[0].files.length; i++) { | |
| if(isAcceptedMimeType(element[0].files.item(i).type)) { | |
| arrayFiles.push(element[0].files.item(i)); | |
| } else { | |
| $inject.feedbackService.addWarning('Arquivo ' + element[0].files.item(i).name + ' não é permitido!'); | |
| } | |
| } | |
| $inject.$parse(attrs.upload).assign(scope, arrayFiles); | |
| scope.$apply(); | |
| }); | |
| } | |
| } | |
| function isAcceptedMimeType(mime) { | |
| var mapMime = {}; | |
| mapMime["image/gif"] = true; | |
| mapMime["image/bmp"] = true; | |
| mapMime["image/png"] = true; | |
| mapMime["image/jpeg"] = true; | |
| mapMime["text/plain"] = true; | |
| mapMime["application/pdf"] = true; | |
| mapMime["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"] = true; //xlsx | |
| mapMime["application/vnd.ms-excel"] = true; //xls | |
| mapMime["application/vnd.oasis.opendocument.spreadsheet"] = true; //ods | |
| mapMime["application/vnd.openxmlformats-officedocument.wordprocessingml.document"] = true; //docx | |
| mapMime["application/msword"] = true; //doc | |
| mapMime["application/vnd.oasis.opendocument.text"] = true; //odt | |
| return mapMime[mime] == undefined ? false : true; | |
| } | |
| export default function directiveFactory($parse, feedbackService) { | |
| 'ngInject' | |
| $inject.$parse = $parse; | |
| $inject.feedbackService = feedbackService; | |
| return new Upload(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment