This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-files-model.js
59 lines (58 loc) · 2.26 KB
/
ng-files-model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @author: Leandro Henrique Reis <[email protected]>
* @date: 2016-10-04 16:05:33
* @last modified by: Leandro Henrique Reis
* @last modified time: 2016-11-13 19:35:53
*/
(function () {
'use strict';
angular.module('ng-files-model', [])
.directive('ngFilesModel', ['$parse', function ($parse) {
return {
restrict: 'A',
scope: {
callback: '=',
data: '=',
model: '='
},
link: function (scope, element, attrs) {
var isMultiple = attrs.multiple;
element.bind('change', function (changeEvent) {
if (isMultiple) {
scope.model=[];
} else {
scope.model={};
}
angular.forEach(element[0].files, function (item, index) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
scope.$apply(function () {
var data={
lastModified: changeEvent.target.files[index].lastModified,
lastModifiedDate: changeEvent.target.files[index].lastModifiedDate,
name: changeEvent.target.files[index].name,
size: changeEvent.target.files[index].size,
type: changeEvent.target.files[index].type,
file: loadEvent.target.result.replace('data:'+changeEvent.target.files[index].type+';base64,','')
}
if (typeof(scope.callback)=='function') {
scope.callback(data, scope.data);
}
if (isMultiple) {
scope.model.push(data);
} else {
scope.model=data;
}
});
}
reader.readAsDataURL(item);
});
});
}
};
}]);
if( typeof exports !== 'undefined' ) {
exports['default'] = angular.module('ng-files-model');
module.exports = exports['default'];
}
})();