Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 649e105

Browse files
AndriiKachurPowerKiKi
authored andcommitted
ability to add http request headers
1 parent fb5692b commit 649e105

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ $uiUploader.startUpload({
9898
});
9999
```
100100

101+
Configure custom request headers by options.headers field
102+
103+
```javascript
104+
$uiUploader.startUpload({
105+
url: 'http://my_domain.com',
106+
concurrency: 2,
107+
headers: {
108+
'Accept': 'application/json'
109+
},
110+
onCompletedAll: function(files) {
111+
// files is an array of File objects
112+
console.log(files);
113+
}
114+
});
115+
```
116+
101117
## Development
102118

103119
We use Karma and jshint to ensure the quality of the code. The easiest way to run these checks is to use grunt:

dist/uploader.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-uploader
33
* https://github.com/angular-ui/ui-uploader
4-
* Version: 1.1.2 - 2015-10-27T03:09:52.784Z
4+
* Version: 1.1.2 - 2015-11-30T14:25:59.048Z
55
* License: MIT
66
*/
77

@@ -41,13 +41,17 @@ function uiUploader($log) {
4141

4242
function startUpload(options) {
4343
self.options = options;
44+
45+
//headers are not shared by requests
46+
var headers = options.headers || {};
47+
4448
for (var i = 0; i < self.files.length; i++) {
4549
if (self.activeUploads == self.options.concurrency) {
4650
break;
4751
}
4852
if (self.files[i].active)
4953
continue;
50-
ajaxUpload(self.files[i], self.options.url, self.options.data);
54+
ajaxUpload(self.files[i], self.options.url, self.options.data, headers);
5155
}
5256
}
5357

@@ -74,12 +78,8 @@ function uiUploader($log) {
7478
return (bytes / Math.pow(1024, i)).toFixed(i ? 1 : 0) + ' ' + sizes[isNaN(bytes) ? 0 : i + 1];
7579
}
7680

77-
function isFunction(entity) {
78-
return typeof(entity) === typeof(Function);
79-
}
80-
81-
function ajaxUpload(file, url, data) {
82-
var xhr, formData, prop, key = '' || 'file';
81+
function ajaxUpload(file, url, data, headers) {
82+
var xhr, formData, prop, key = 'file';
8383
data = data || {};
8484

8585
self.activeUploads += 1;
@@ -94,6 +94,14 @@ function uiUploader($log) {
9494
formData = new window.FormData();
9595
xhr.open('POST', url);
9696

97+
if (headers) {
98+
for (var headerKey in headers) {
99+
if (headers.hasOwnProperty(headerKey)) {
100+
xhr.setRequestHeader(headerKey, headers[headerKey]);
101+
}
102+
}
103+
}
104+
97105
// Triggered when upload starts:
98106
xhr.upload.onloadstart = function() {
99107
};
@@ -109,7 +117,7 @@ function uiUploader($log) {
109117
//console.info(event.loaded);
110118
file.loaded = event.loaded;
111119
file.humanSize = getHumanSize(event.loaded);
112-
if (isFunction(self.options.onProgress)) {
120+
if (angular.isFunction(self.options.onProgress)) {
113121
self.options.onProgress(file);
114122
}
115123
};
@@ -119,20 +127,20 @@ function uiUploader($log) {
119127
self.activeUploads -= 1;
120128
self.uploadedFiles += 1;
121129
startUpload(self.options);
122-
if (isFunction(self.options.onCompleted)) {
130+
if (angular.isFunction(self.options.onCompleted)) {
123131
self.options.onCompleted(file, xhr.responseText, xhr.status);
124132
}
125133
if (self.uploadedFiles === self.files.length) {
126134
self.uploadedFiles = 0;
127-
if (isFunction(self.options.onCompletedAll)) {
135+
if (angular.isFunction(self.options.onCompletedAll)) {
128136
self.options.onCompletedAll(self.files);
129137
}
130138
}
131139
};
132140

133141
// Triggered when upload fails:
134142
xhr.onerror = function(e) {
135-
if (isFunction(self.options.onError)) {
143+
if (angular.isFunction(self.options.onError)) {
136144
self.options.onError(e);
137145
}
138146
};

dist/uploader.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uploader.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ function uiUploader($log) {
3131

3232
function startUpload(options) {
3333
self.options = options;
34+
35+
//headers are not shared by requests
36+
var headers = options.headers || {};
37+
3438
for (var i = 0; i < self.files.length; i++) {
3539
if (self.activeUploads == self.options.concurrency) {
3640
break;
3741
}
3842
if (self.files[i].active)
3943
continue;
40-
ajaxUpload(self.files[i], self.options.url, self.options.data);
44+
ajaxUpload(self.files[i], self.options.url, self.options.data, headers);
4145
}
4246
}
4347

@@ -64,7 +68,7 @@ function uiUploader($log) {
6468
return (bytes / Math.pow(1024, i)).toFixed(i ? 1 : 0) + ' ' + sizes[isNaN(bytes) ? 0 : i + 1];
6569
}
6670

67-
function ajaxUpload(file, url, data) {
71+
function ajaxUpload(file, url, data, headers) {
6872
var xhr, formData, prop, key = 'file';
6973
data = data || {};
7074

@@ -80,6 +84,14 @@ function uiUploader($log) {
8084
formData = new window.FormData();
8185
xhr.open('POST', url);
8286

87+
if (headers) {
88+
for (var headerKey in headers) {
89+
if (headers.hasOwnProperty(headerKey)) {
90+
xhr.setRequestHeader(headerKey, headers[headerKey]);
91+
}
92+
}
93+
}
94+
8395
// Triggered when upload starts:
8496
xhr.upload.onloadstart = function() {
8597
};

0 commit comments

Comments
 (0)