Skip to content

Commit

Permalink
Merge pull request #784 from jaisonerick/master
Browse files Browse the repository at this point in the history
Using usual forms request form for fields
  • Loading branch information
danialfarid committed Jun 4, 2015
2 parents f1e74c3 + 232c451 commit 30216cb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions demo/war/js/ng-file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ ngFileUpload.service('Upload', ['$http', '$q', '$timeout', function ($http, $q,
return promise;
}

function addFormData(root, lastKey, formData) {
if(angular.isDate(root)) {
return formData.append(lastKey, root.toISOString());
}
if(angular.isString(root) || angular.isNumber(root)) {
return formData.append(lastKey, root);
}
if(angular.isObject(root)) {
for(key in root) {
if(root.hasOwnProperty(key)) {
var val = root[key];
getData(val, lastKey + '[' + key + ']', formData);
}
}
return ;
}
if(angular.isArray(root)) {
for(var i=0; i < root.length; i++) {
getData(root[i], lastKey + '[' + i + ']', formData);
}
return ;
}
formData.append(lastKey, JSON.stringify(root));
}

this.upload = function (config) {
config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
Expand Down Expand Up @@ -150,6 +175,8 @@ ngFileUpload.service('Upload', ['$http', '$q', '$timeout', function ($http, $q,
} else {
if (config.sendObjectsAsJsonBlob && angular.isObject(val)) {
formData.append(key, new Blob([val], {type: 'application/json'}));
} else if (config.sendFieldsAsFormValue && (angular.isArray(val) || angular.isObject(val))) {
addFormData(val, key, formData);
} else {
formData.append(key, JSON.stringify(val));
}
Expand Down

0 comments on commit 30216cb

Please sign in to comment.