//Following function will return formdata for your javascript object
//It will process Array and File Data Types also.
//It will process Array and File Data Types also.
function GenerateFormData(obj, form, Namespace) {
var ObjKeys = Object.keys(obj)
var formData = form || new FormData();
$.each(ObjKeys, function (index, key) {
if (obj[key] instanceof Array) {
for (var i = 0; i < obj[key].length; i++) {
var ObjPrefix = "";
if (Namespace)
{
ObjPrefix = Namespace + "." + key + "[" + i + "]";
}
else {
ObjPrefix = key + "[" + i + "]";
}
if (obj[key][i] instanceof File) {
formData.append(ObjPrefix, obj[key][i]);
}
else if (obj[key][i] instanceof Object) {
formData = GenerateFormData(obj[key][i], formData, ObjPrefix);
}
else {
formData.append(ObjPrefix, obj[key][i]);
}
}
}
else {
var ObjPrefix = "";
if (Namespace) {
ObjPrefix = Namespace + "." + key;
}
else {
ObjPrefix = key;
}
if (obj[key] instanceof File) {
formData.append(ObjPrefix, obj[key]);
}
else if (obj[key] instanceof Object) {
formData = GenerateFormData(obj[key], formData, ObjPrefix);
}
else {
formData.append(ObjPrefix, obj[key]);
}
}
});
return formData;
}
var obj={ID:10,Name:"Udayakumar",Age:18,Subject:["Tamil","English","Maths","Science"]};
var UmDataCollection=GenerateFormData(obj,null,"model");