I'm Developing with Appcelerator SDK 1.6.2 for iOS 4.3
I'm trying to upload multiple files to a server, the problem is the number of files is dynamic and therefore cant be predetermined in the params of the XHR send.
If I pass one file in it works fine but I cant seem to figure out how to pass in many.
I've tried creating an array to hold the media elements but no dice.
var media = [];
for(var i = 0; i < sync.images.length; i++){
media[i] = Titanium.Filesystem.getFile(sync.images[i].path).read();
}
xhr.send({
media: media // no workie
// media: media[1] workie
});
I found this article: http://developer.appcelerator.com/question/123794/multiple-file-upload-in-one-request
that assumes the following code should work:
xhr.send({
'media[]': imageFile.read(),
'media[]': imageFile2.read()
});
however i'm uncertain how to obtain this dynamically due to the fact that the number of images transferred can vary
Suggestions would be great
I had this exact problem, and I ended up using the Object.defineProperty method. Here's how your code could be modified:
Change media into an object instead of an array, then iteratively define properties:
var media = {};
for(var i = 0; i < sync.images.length; i++){
data_blob = Titanium.Filesystem.getFile(sync.images[i].path).read();
data_key = 'image'+i;
Object.defineProperty(media, data_key, {value: data_blob, enumerable: true});
}
xhr.send(media);
Here's the mozilla doc for defineProperty: