forked from danialfarid/ng-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-file-upload.js
126 lines (116 loc) · 3.3 KB
/
angular-file-upload.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**!
* AngularJS file upload/drop directive with http post and progress
* @author Danial <[email protected]>
* @version 1.1.4
*/
(function() {
var angularFileUpload = angular.module('angularFileUpload', []);
angularFileUpload.service('$upload', ['$http', function($http) {
this.upload = function(config) {
config.method = config.method || 'POST';
config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
var formData = new FormData();
if (config.data) {
for (var key in config.data) {
var val = config.data[key];
if (config.transformRequest) {
if (typeof config.transformRequest == 'function') {
val = config.transformRequest(val);
} else {
for (fn in config.transformRequest) {
if (typeof fn == 'function') {
val = fn(val);
}
}
}
} else {
val = $http.defaults.transformRequest[0](val);
}
formData.append(key, val);
}
}
config.transformRequest = angular.identity;
formData.append(config.fileFormDataName || 'file', config.file, config.file.name);
formData['__uploadProgress_'] = function(e) {
if (e) config.progress(e);
};
config.data = formData;
var response = $http(config);
response.abort = function(){throw "upload is not started yet"};
formData['__setAbortFunction_'] = function(fn) {
response.abort = fn;
}
return response;
};
}]);
angularFileUpload.directive('ngFileSelect', [ '$parse', '$http', function($parse, $http) {
return function(scope, elem, attr) {
var fn = $parse(attr['ngFileSelect']);
elem.bind('change', function(evt) {
var files = [], fileList, i;
fileList = evt.target.files;
if (fileList != null) {
for (i = 0; i < fileList.length; i++) {
files.push(fileList.item(i));
}
}
scope.$apply(function() {
fn(scope, {
$files : files,
$event : evt
});
});
});
elem.bind('click', function(){
this.value = null;
});
};
} ]);
angularFileUpload.directive('ngFileDropAvailable', [ '$parse', '$http', function($parse, $http) {
return function(scope, elem, attr) {
if ('draggable' in document.createElement('span')) {
var fn = $parse(attr['ngFileDropAvailable']);
if(!scope.$$phase) {
scope.$apply(function() {
fn(scope);
});
} else {
fn(scope)
}
}
};
} ]);
angularFileUpload.directive('ngFileDrop', [ '$parse', '$http', function($parse, $http) {
return function(scope, elem, attr) {
if ('draggable' in document.createElement('span')) {
var fn = $parse(attr['ngFileDrop']);
elem[0].addEventListener("dragover", function(evt) {
evt.stopPropagation();
evt.preventDefault();
elem.addClass(attr['ngFileDragOverClass'] || "dragover");
}, false);
elem[0].addEventListener("dragleave", function(evt) {
elem.removeClass(attr['ngFileDragOverClass'] || "dragover");
}, false);
elem[0].addEventListener("drop", function(evt) {
evt.stopPropagation();
evt.preventDefault();
elem.removeClass(attr['ngFileDragOverClass'] || "dragover");
var files = [], fileList = evt.dataTransfer.files, i;
if (fileList != null) {
for (i = 0; i < fileList.length; i++) {
files.push(fileList.item(i));
}
}
scope.$apply(function() {
fn(scope, {
$files : files,
$event : evt
});
});
}, false);
}
};
} ]);
})();