Skip to content

Commit 49d1d4b

Browse files
Danial FaridDanial Farid
Danial Farid
authored and
Danial Farid
committed
Redone the file option
1 parent cdccedf commit 49d1d4b

19 files changed

+162
-83
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,14 @@ Drop files here
223223
```js
224224
var upload = Upload.upload({
225225
*url: 'server/upload/url', // upload.php script, node.js route, or servlet url
226-
*file: file, // single file or an array of files (array is for html5 only)
226+
*file: file or files or {pic: picFile, doc: docFile},
227+
// single file or an array of files (html5 only) or
228+
// a map of key[,name] -> file (map with more than one entry is for html5 only)
229+
// the key is server request file form key param ('Content-Disposition') and
230+
// the optional comma-separated name (html5 only) is to chnage the original file name.
231+
// by default the key is 'file' and original file name is used.
227232
method: 'POST' or 'PUT'(html5), default POST,
228233
headers: {'Authorization': 'xxx'}, // only for html5
229-
fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...], // to modify the name of the file(s)
230-
/*
231-
file formData name ('Content-Disposition'), server side request file parameter name could be
232-
an array of names for multiple files (html5). Default is 'file' */
233-
fileFormDataName: 'myFile' or ['file[0]', 'file[1]', ...],
234234
/*
235235
map of extra form data fields to send along with file. each field will be sent as a form field.
236236
The values are converted to json string or jsob blob or nested form depending on 'sendFieldsAs' option. */

demo/src/main/webapp/js/FileAPI.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/src/main/webapp/js/ng-file-upload-all.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* AngularJS file upload/drop directive and service with progress and abort
33
* FileAPI Flash shim for old browsers not supporting FormData
44
* @author Danial <[email protected]>
5-
* @version 7.1.0
5+
* @version 7.2.0
66
*/
77

88
(function () {
@@ -422,7 +422,7 @@ if (!window.FileReader) {
422422
/**!
423423
* AngularJS file upload/drop directive and service with progress and abort
424424
* @author Danial <[email protected]>
425-
* @version 7.1.0
425+
* @version 7.2.0
426426
*/
427427

428428
if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
@@ -443,7 +443,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
443443

444444
var ngFileUpload = angular.module('ngFileUpload', []);
445445

446-
ngFileUpload.version = '7.1.0';
446+
ngFileUpload.version = '7.2.0';
447447

448448
ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
449449
function sendHttp(config) {
@@ -563,15 +563,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
563563
}
564564
}
565565

566+
function isFile(file) {
567+
return file instanceof Blob || (file.flashId && file.name && file.size);
568+
}
569+
570+
function addFileToFormData(formData, file, key) {
571+
if (isFile(file)) {
572+
formData.append(key, file, file.fileName || file.name);
573+
} else if (angular.isObject(file)) {
574+
for (var k in file) {
575+
if (file.hasOwnProperty(k)) {
576+
var split = k.split(',');
577+
if (split[1]) {
578+
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
579+
}
580+
addFileToFormData(formData, file[k], split[0]);
581+
}
582+
}
583+
} else {
584+
throw 'Expected file object in Upload.upload file option: ' + file.toString();
585+
}
586+
}
587+
566588
config.headers = config.headers || {};
567589
config.headers['Content-Type'] = undefined;
568590
config.transformRequest = config.transformRequest ?
569591
(angular.isArray(config.transformRequest) ?
570592
config.transformRequest : [config.transformRequest]) : [];
571593
config.transformRequest.push(function (data) {
572-
var formData = new FormData();
573-
var allFields = {};
574-
var key;
594+
var formData = new FormData(), allFields = {}, key;
575595
for (key in config.fields) {
576596
if (config.fields.hasOwnProperty(key)) {
577597
allFields[key] = config.fields[key];
@@ -590,16 +610,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
590610
}
591611

592612
if (config.file != null) {
593-
var fileFormName = config.fileFormDataName || 'file';
594-
595613
if (angular.isArray(config.file)) {
596-
var isFileFormNameString = angular.isString(fileFormName);
597614
for (var i = 0; i < config.file.length; i++) {
598-
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
599-
(config.fileName && config.fileName[i]) || config.file[i].name);
615+
addFileToFormData(formData, config.file[i], 'file');
600616
}
601617
} else {
602-
formData.append(fileFormName, config.file, config.fileName || config.file.name);
618+
addFileToFormData(formData, config.file, 'file');
603619
}
604620
}
605621
return formData;
@@ -618,7 +634,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
618634
return sendHttp(config);
619635
};
620636

621-
this.setDefaults = function(defaults) {
637+
this.setDefaults = function (defaults) {
622638
this.defaults = defaults || {};
623639
};
624640

demo/src/main/webapp/js/ng-file-upload-all.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.

demo/src/main/webapp/js/ng-file-upload-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* AngularJS file upload/drop directive and service with progress and abort
33
* FileAPI Flash shim for old browsers not supporting FormData
44
* @author Danial <[email protected]>
5-
* @version 7.1.0
5+
* @version 7.2.0
66
*/
77

88
(function () {

demo/src/main/webapp/js/ng-file-upload-shim.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/src/main/webapp/js/ng-file-upload.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**!
22
* AngularJS file upload/drop directive and service with progress and abort
33
* @author Danial <[email protected]>
4-
* @version 7.1.0
4+
* @version 7.2.0
55
*/
66

77
if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
@@ -22,7 +22,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
2222

2323
var ngFileUpload = angular.module('ngFileUpload', []);
2424

25-
ngFileUpload.version = '7.1.0';
25+
ngFileUpload.version = '7.2.0';
2626

2727
ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
2828
function sendHttp(config) {
@@ -142,15 +142,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
142142
}
143143
}
144144

145+
function isFile(file) {
146+
return file instanceof Blob || (file.flashId && file.name && file.size);
147+
}
148+
149+
function addFileToFormData(formData, file, key) {
150+
if (isFile(file)) {
151+
formData.append(key, file, file.fileName || file.name);
152+
} else if (angular.isObject(file)) {
153+
for (var k in file) {
154+
if (file.hasOwnProperty(k)) {
155+
var split = k.split(',');
156+
if (split[1]) {
157+
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
158+
}
159+
addFileToFormData(formData, file[k], split[0]);
160+
}
161+
}
162+
} else {
163+
throw 'Expected file object in Upload.upload file option: ' + file.toString();
164+
}
165+
}
166+
145167
config.headers = config.headers || {};
146168
config.headers['Content-Type'] = undefined;
147169
config.transformRequest = config.transformRequest ?
148170
(angular.isArray(config.transformRequest) ?
149171
config.transformRequest : [config.transformRequest]) : [];
150172
config.transformRequest.push(function (data) {
151-
var formData = new FormData();
152-
var allFields = {};
153-
var key;
173+
var formData = new FormData(), allFields = {}, key;
154174
for (key in config.fields) {
155175
if (config.fields.hasOwnProperty(key)) {
156176
allFields[key] = config.fields[key];
@@ -169,16 +189,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
169189
}
170190

171191
if (config.file != null) {
172-
var fileFormName = config.fileFormDataName || 'file';
173-
174192
if (angular.isArray(config.file)) {
175-
var isFileFormNameString = angular.isString(fileFormName);
176193
for (var i = 0; i < config.file.length; i++) {
177-
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
178-
(config.fileName && config.fileName[i]) || config.file[i].name);
194+
addFileToFormData(formData, config.file[i], 'file');
179195
}
180196
} else {
181-
formData.append(fileFormName, config.file, config.fileName || config.file.name);
197+
addFileToFormData(formData, config.file, 'file');
182198
}
183199
}
184200
return formData;
@@ -197,7 +213,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
197213
return sendHttp(config);
198214
};
199215

200-
this.setDefaults = function(defaults) {
216+
this.setDefaults = function (defaults) {
201217
this.defaults = defaults || {};
202218
};
203219

demo/src/main/webapp/js/ng-file-upload.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.

demo/src/main/webapp/js/upload.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ app.controller('MyCtrl', ['$scope', '$http', '$timeout', '$compile', 'Upload', f
6161
},
6262
fields: {username: $scope.username},
6363
file: file,
64-
fileFormDataName: 'myFile'
6564
});
6665

6766
file.upload.then(function (response) {

dist/FileAPI.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ng-file-upload-all.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* AngularJS file upload/drop directive and service with progress and abort
33
* FileAPI Flash shim for old browsers not supporting FormData
44
* @author Danial <[email protected]>
5-
* @version 7.1.0
5+
* @version 7.2.0
66
*/
77

88
(function () {
@@ -422,7 +422,7 @@ if (!window.FileReader) {
422422
/**!
423423
* AngularJS file upload/drop directive and service with progress and abort
424424
* @author Danial <[email protected]>
425-
* @version 7.1.0
425+
* @version 7.2.0
426426
*/
427427

428428
if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
@@ -443,7 +443,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
443443

444444
var ngFileUpload = angular.module('ngFileUpload', []);
445445

446-
ngFileUpload.version = '7.1.0';
446+
ngFileUpload.version = '7.2.0';
447447

448448
ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
449449
function sendHttp(config) {
@@ -563,15 +563,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
563563
}
564564
}
565565

566+
function isFile(file) {
567+
return file instanceof Blob || (file.flashId && file.name && file.size);
568+
}
569+
570+
function addFileToFormData(formData, file, key) {
571+
if (isFile(file)) {
572+
formData.append(key, file, file.fileName || file.name);
573+
} else if (angular.isObject(file)) {
574+
for (var k in file) {
575+
if (file.hasOwnProperty(k)) {
576+
var split = k.split(',');
577+
if (split[1]) {
578+
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
579+
}
580+
addFileToFormData(formData, file[k], split[0]);
581+
}
582+
}
583+
} else {
584+
throw 'Expected file object in Upload.upload file option: ' + file.toString();
585+
}
586+
}
587+
566588
config.headers = config.headers || {};
567589
config.headers['Content-Type'] = undefined;
568590
config.transformRequest = config.transformRequest ?
569591
(angular.isArray(config.transformRequest) ?
570592
config.transformRequest : [config.transformRequest]) : [];
571593
config.transformRequest.push(function (data) {
572-
var formData = new FormData();
573-
var allFields = {};
574-
var key;
594+
var formData = new FormData(), allFields = {}, key;
575595
for (key in config.fields) {
576596
if (config.fields.hasOwnProperty(key)) {
577597
allFields[key] = config.fields[key];
@@ -590,16 +610,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
590610
}
591611

592612
if (config.file != null) {
593-
var fileFormName = config.fileFormDataName || 'file';
594-
595613
if (angular.isArray(config.file)) {
596-
var isFileFormNameString = angular.isString(fileFormName);
597614
for (var i = 0; i < config.file.length; i++) {
598-
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
599-
(config.fileName && config.fileName[i]) || config.file[i].name);
615+
addFileToFormData(formData, config.file[i], 'file');
600616
}
601617
} else {
602-
formData.append(fileFormName, config.file, config.fileName || config.file.name);
618+
addFileToFormData(formData, config.file, 'file');
603619
}
604620
}
605621
return formData;
@@ -618,7 +634,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
618634
return sendHttp(config);
619635
};
620636

621-
this.setDefaults = function(defaults) {
637+
this.setDefaults = function (defaults) {
622638
this.defaults = defaults || {};
623639
};
624640

dist/ng-file-upload-all.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.

dist/ng-file-upload-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* AngularJS file upload/drop directive and service with progress and abort
33
* FileAPI Flash shim for old browsers not supporting FormData
44
* @author Danial <[email protected]>
5-
* @version 7.1.0
5+
* @version 7.2.0
66
*/
77

88
(function () {

0 commit comments

Comments
 (0)