I receive a list of files from the phone, and then I need to send data to the server, but since the data I have is not one thing, they are sent all separately, how can I collect them to a heap? I send it with jQuery like this:

function listResults(entries) { dir_i = 0; file_i = 0; imageURI = entries[0].toURL(entry.name); options = new FileUploadOptions(); options.chunkedMode = false; options.fileKey = "text"; options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); //alert(options.fileName); options.mimeType = "text/plain"; params = new Object(); entries.forEach(function (entry) { if (entry.isDirectory) { params.dir_[dir_i] = entry.name; dir_i++; } else { params.file_[file_i] = entry.name; file_i++; } }); options.params = params; $.post("http://91.228.199.95/ksiywFac63f2hs/userfotos.php", { dir:options }, function (data) { // alert("Data Loaded: " + data); }); } 

entry.name - a list of file names that I get using DirectoryReader'a

  • one
    something is not very clear piece of code, can therefore minus - sercxjo
  • one
    How did you understand that the data did not come? - Zowie
  • one
    How do you check the data? what, at the time of sending, does the result variable contain? Or is it casting on the battle of psychics? - Zowie
  • one
    @dajver - it's better, I would like to correct the indents - Zowie
  • 2
    Something I am not sure that you want exactly this: if (entry.isDirectory) {params.dir_ [dir_i] = entry.name; dir_i ++; } else {params.file_ [file_i] = entry.name; file_i ++; } As I understand it, you want to do something like if (entry.isDirectory) {params ['dir_' + dir_i] = entry.name; dir_i ++; } else {params ['file_' + file_i] = entry.name; file_i ++; } By the way, it is inconvenient to work with such data. And the most important thing is to discover JSON for yourself :) PS: the impossibility of editing comments has already reached, I’ve got Manal - Zowie

1 answer 1

Maybe you want it like this: (initialization dir_i , file_i , params , options rendered in listResults , sending data to the server too)

 function listResults(entries) { dir_i = 0; file_i = 0; imageURI = entries[0].toURL(entry.name); options = new FileUploadOptions(); options.chunkedMode = false; options.fileKey = "text"; options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); //alert(options.fileName); options.mimeType = "text/plain"; params = new Object(); entries.forEach(function (entry) { if (entry.isDirectory) { params.dir_[dir_i] = entry.name; dir_i++; } else { params.file_[file_i] = entry.name; file_i++; } }); options.params = params; $.post("http://91.228.199.95/ksiywFac63f2hs/userfotos.php", { dir:options }, function (data) { // alert("Data Loaded: " + data); }); } 

And then you pushed a lot under forEach ...

  • do console.log (options); and nothing goes to the log, but the idea is to have a line with a list of folders. Where is the mistake? - dajver
  • one
    Yes, it seems to me the trouble is that forEach in another thread goes? or I'm wrong? - Chad
  • one
    Or maybe there is a thread like entries.forEach (function (entry) {...}). Done (function ...); ? Well, I would give the names a few other options and params - otherwise we use it as a global variable, who wipes it? Chad
  • one
    Arrange the alerts in the forex, after him, to understand the progress of the code. You need to find out a few points: 1. That forEach is executed 2. That forEach is executed in the same thread and not asynchronously 3. That the output of params and options.params do not break - Chad
  • one
    so the console and nothing else to say for the object .... - Chad