jquery - How can i read an object filelist array in javascript? -
i have created array store list of selected files. well, have honest, looked code , when realized worked purpose, used it. need access array in order delete files manually, have tried using index of each sector, not work.
this how create array , store files.
var files = []; $("button:first").on("click", function(e) { $("<input>").prop({ "type": "file", "multiple": true }).on("change", function(e) { files.push(this.files); }).trigger("click"); });
how read array files[] if contains object filelist or obtain indexs array?
here's how understand code:
each time first button in dom clicked file input dialogue accepts multiple files generated. upon homecoming dialogue emits change
event files
variable (a filelist object) attached function context (this
). code pushes newly created filelist onto files
array. since input accepts multiple files each object pushed onto files
array filelist object.
so if want iterate through elements in files array can set function in change
event handler:
var files = []; $("button:first").on("click", function(e) { $("<input>").prop({ "type": "file", "multiple": true }).on("change", function(e) { files.push(this.files); iteratefiles(files); }).trigger("click"); }); function iteratefiles(filesarray) { for(var i=0; i<filesarray.length; i++){ for(var j=0; j<filesarray[i].length; j++){ console.log(filesarray[i][j].name); // alternatively: console.log(filesarray[i].item(j).name); } } }
in iteratefiles()
function wrote filesarray[i][j]
isn't multidimensional array -- rather single dimensional array containing filelist objects behave much arrays...except can't delete/splice items out of them -- read-only.
for more info on why can't delete see: how remove file filelist
javascript jquery arrays html5 arraylist
No comments:
Post a Comment