Skip to content

Commit

Permalink
Redone the file option
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial Farid authored and Danial Farid committed Sep 12, 2015
1 parent cdccedf commit 49d1d4b
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 83 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ Drop files here
```js
var upload = Upload.upload({
*url: 'server/upload/url', // upload.php script, node.js route, or servlet url
*file: file, // single file or an array of files (array is for html5 only)
*file: file or files or {pic: picFile, doc: docFile},
// single file or an array of files (html5 only) or
// a map of key[,name] -> file (map with more than one entry is for html5 only)
// the key is server request file form key param ('Content-Disposition') and
// the optional comma-separated name (html5 only) is to chnage the original file name.
// by default the key is 'file' and original file name is used.
method: 'POST' or 'PUT'(html5), default POST,
headers: {'Authorization': 'xxx'}, // only for html5
fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...], // to modify the name of the file(s)
/*
file formData name ('Content-Disposition'), server side request file parameter name could be
an array of names for multiple files (html5). Default is 'file' */
fileFormDataName: 'myFile' or ['file[0]', 'file[1]', ...],
/*
map of extra form data fields to send along with file. each field will be sent as a form field.
The values are converted to json string or jsob blob or nested form depending on 'sendFieldsAs' option. */
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/FileAPI.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 29 additions & 13 deletions demo/src/main/webapp/js/ng-file-upload-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* AngularJS file upload/drop directive and service with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

(function () {
Expand Down Expand Up @@ -422,7 +422,7 @@ if (!window.FileReader) {
/**!
* AngularJS file upload/drop directive and service with progress and abort
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

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

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

ngFileUpload.version = '7.1.0';
ngFileUpload.version = '7.2.0';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
function sendHttp(config) {
Expand Down Expand Up @@ -563,15 +563,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}
}

function isFile(file) {
return file instanceof Blob || (file.flashId && file.name && file.size);
}

function addFileToFormData(formData, file, key) {
if (isFile(file)) {
formData.append(key, file, file.fileName || file.name);
} else if (angular.isObject(file)) {
for (var k in file) {
if (file.hasOwnProperty(k)) {
var split = k.split(',');
if (split[1]) {
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
}
addFileToFormData(formData, file[k], split[0]);
}
}
} else {
throw 'Expected file object in Upload.upload file option: ' + file.toString();
}
}

config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
config.transformRequest = config.transformRequest ?
(angular.isArray(config.transformRequest) ?
config.transformRequest : [config.transformRequest]) : [];
config.transformRequest.push(function (data) {
var formData = new FormData();
var allFields = {};
var key;
var formData = new FormData(), allFields = {}, key;
for (key in config.fields) {
if (config.fields.hasOwnProperty(key)) {
allFields[key] = config.fields[key];
Expand All @@ -590,16 +610,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}

if (config.file != null) {
var fileFormName = config.fileFormDataName || 'file';

if (angular.isArray(config.file)) {
var isFileFormNameString = angular.isString(fileFormName);
for (var i = 0; i < config.file.length; i++) {
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
(config.fileName && config.fileName[i]) || config.file[i].name);
addFileToFormData(formData, config.file[i], 'file');
}
} else {
formData.append(fileFormName, config.file, config.fileName || config.file.name);
addFileToFormData(formData, config.file, 'file');
}
}
return formData;
Expand All @@ -618,7 +634,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return sendHttp(config);
};

this.setDefaults = function(defaults) {
this.setDefaults = function (defaults) {
this.defaults = defaults || {};
};

Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/webapp/js/ng-file-upload-all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* AngularJS file upload/drop directive and service with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

(function () {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 28 additions & 12 deletions demo/src/main/webapp/js/ng-file-upload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* AngularJS file upload/drop directive and service with progress and abort
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

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

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

ngFileUpload.version = '7.1.0';
ngFileUpload.version = '7.2.0';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
function sendHttp(config) {
Expand Down Expand Up @@ -142,15 +142,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}
}

function isFile(file) {
return file instanceof Blob || (file.flashId && file.name && file.size);
}

function addFileToFormData(formData, file, key) {
if (isFile(file)) {
formData.append(key, file, file.fileName || file.name);
} else if (angular.isObject(file)) {
for (var k in file) {
if (file.hasOwnProperty(k)) {
var split = k.split(',');
if (split[1]) {
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
}
addFileToFormData(formData, file[k], split[0]);
}
}
} else {
throw 'Expected file object in Upload.upload file option: ' + file.toString();
}
}

config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
config.transformRequest = config.transformRequest ?
(angular.isArray(config.transformRequest) ?
config.transformRequest : [config.transformRequest]) : [];
config.transformRequest.push(function (data) {
var formData = new FormData();
var allFields = {};
var key;
var formData = new FormData(), allFields = {}, key;
for (key in config.fields) {
if (config.fields.hasOwnProperty(key)) {
allFields[key] = config.fields[key];
Expand All @@ -169,16 +189,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}

if (config.file != null) {
var fileFormName = config.fileFormDataName || 'file';

if (angular.isArray(config.file)) {
var isFileFormNameString = angular.isString(fileFormName);
for (var i = 0; i < config.file.length; i++) {
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
(config.fileName && config.fileName[i]) || config.file[i].name);
addFileToFormData(formData, config.file[i], 'file');
}
} else {
formData.append(fileFormName, config.file, config.fileName || config.file.name);
addFileToFormData(formData, config.file, 'file');
}
}
return formData;
Expand All @@ -197,7 +213,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return sendHttp(config);
};

this.setDefaults = function(defaults) {
this.setDefaults = function (defaults) {
this.defaults = defaults || {};
};

Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/webapp/js/ng-file-upload.min.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion demo/src/main/webapp/js/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ app.controller('MyCtrl', ['$scope', '$http', '$timeout', '$compile', 'Upload', f
},
fields: {username: $scope.username},
file: file,
fileFormDataName: 'myFile'
});

file.upload.then(function (response) {
Expand Down
2 changes: 1 addition & 1 deletion dist/FileAPI.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 29 additions & 13 deletions dist/ng-file-upload-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* AngularJS file upload/drop directive and service with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

(function () {
Expand Down Expand Up @@ -422,7 +422,7 @@ if (!window.FileReader) {
/**!
* AngularJS file upload/drop directive and service with progress and abort
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

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

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

ngFileUpload.version = '7.1.0';
ngFileUpload.version = '7.2.0';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
function sendHttp(config) {
Expand Down Expand Up @@ -563,15 +563,35 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}
}

function isFile(file) {
return file instanceof Blob || (file.flashId && file.name && file.size);
}

function addFileToFormData(formData, file, key) {
if (isFile(file)) {
formData.append(key, file, file.fileName || file.name);
} else if (angular.isObject(file)) {
for (var k in file) {
if (file.hasOwnProperty(k)) {
var split = k.split(',');
if (split[1]) {
file[k].fileName = split[1].replace(/^\s+|\s+$/g, '');
}
addFileToFormData(formData, file[k], split[0]);
}
}
} else {
throw 'Expected file object in Upload.upload file option: ' + file.toString();
}
}

config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
config.transformRequest = config.transformRequest ?
(angular.isArray(config.transformRequest) ?
config.transformRequest : [config.transformRequest]) : [];
config.transformRequest.push(function (data) {
var formData = new FormData();
var allFields = {};
var key;
var formData = new FormData(), allFields = {}, key;
for (key in config.fields) {
if (config.fields.hasOwnProperty(key)) {
allFields[key] = config.fields[key];
Expand All @@ -590,16 +610,12 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}

if (config.file != null) {
var fileFormName = config.fileFormDataName || 'file';

if (angular.isArray(config.file)) {
var isFileFormNameString = angular.isString(fileFormName);
for (var i = 0; i < config.file.length; i++) {
formData.append(isFileFormNameString ? fileFormName : fileFormName[i], config.file[i],
(config.fileName && config.fileName[i]) || config.file[i].name);
addFileToFormData(formData, config.file[i], 'file');
}
} else {
formData.append(fileFormName, config.file, config.fileName || config.file.name);
addFileToFormData(formData, config.file, 'file');
}
}
return formData;
Expand All @@ -618,7 +634,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return sendHttp(config);
};

this.setDefaults = function(defaults) {
this.setDefaults = function (defaults) {
this.defaults = defaults || {};
};

Expand Down
4 changes: 2 additions & 2 deletions dist/ng-file-upload-all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ng-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* AngularJS file upload/drop directive and service with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 7.1.0
* @version 7.2.0
*/

(function () {
Expand Down
Loading

0 comments on commit 49d1d4b

Please sign in to comment.