Issue
I'm using ajax to upload files. To pass and save the data I'm using the FormData object. I would like to show the length of the FormData object but I can not do it.
I tried in this ways
var data = new FormData();
jQuery.each($(this)[0].files, function(i, file) {
data.append('img['+i+']', file);
});
/* FIRST */
var getObjectSize = function(obj) {
var leng = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) leng++;
}
return leng;
};
var items = getObjectSize(data);
alert(items); // output 0
/* SECOND */
var items = Object.keys(data).length;
alert(items); // output 0
How can I do this? Thanks.
Solution
Formdata looks like this , when you use console.log(formData),
FormData {
append: function
}
__proto__: FormDataappend: function append() {
[native code]
}
arguments: nullcaller: nulllength: 2name: "append"
__proto__: function Empty() {}
apply: function apply() {
[native code]
}
arguments: nullbind: function bind() {
[native code]
}
call: function call() {
[native code]
}
caller: nullconstructor: function Function() {
[native code]
}
length: 0name: "Empty"
toString: function toString() {
[native code]
}
__proto__: Object < function scope > < function scope > Global: Windowconstructor: function FormData() {
[native code]
}
__proto__: Object__defineGetter__: function __defineGetter__() {
[native code]
}
__defineSetter__: function __defineSetter__() {
[native code]
}
__lookupGetter__: function __lookupGetter__() {
[native code]
}
__lookupSetter__: function __lookupSetter__() {
[native code]
}
constructor: function Object() {
[native code]
}
hasOwnProperty: function hasOwnProperty() {
[native code]
}
isPrototypeOf: function isPrototypeOf() {
[native code]
}
propertyIsEnumerable: function propertyIsEnumerable() {
[native code]
}
toLocaleString: function toLocaleString() {
[native code]
}
toString: function toString() {
[native code]
}
valueOf: function valueOf() {
[native code]
}
get __proto__: function __proto__() {
[native code]
}
set __proto__: function __proto__() {
[native code]
}
So, There is no getting of interrogating with the data that has been stored to FormData
. And, The official document mention the same.
So, instead of that why not fetch the length , by directly counting file numbers
var length=$(this).get(0).files.length
P.S : And you can get more detail though this question/answer.
Answered By - Runcorn Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.