Skip to content

Instantly share code, notes, and snippets.

@AlaaAttya
Last active October 19, 2016 12:44
Show Gist options
  • Save AlaaAttya/e147bbb31a492486189f5f0665d9fc58 to your computer and use it in GitHub Desktop.
Save AlaaAttya/e147bbb31a492486189f5f0665d9fc58 to your computer and use it in GitHub Desktop.
upload multiple parse files using promises
/**
* Upload multiple images to parse
* @author Alaa Attya <alaa.attya91@gmail.com>
*
* @param {Arrya} file_elements
* @param {function} callback
* @return {void}
*/
function upload_parse_images(file_elements, callback) {
var file_promises = [];
var files_array = [];
file_elements.forEach(function(file_item) {
var file_element = file_item.file_obj;
var fileUploadControl = file_element[0];
if(fileUploadControl && fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = file.name; //This does *NOT* need to be a unique name
var parseFile = new Parse.File(name, file);
file_promises.push(parseFile.save().then(function() {
files_array.push({'parse_file': parseFile, 'attr_name': file_item.attr_name});
}));
}
});
Parse.Promise.when(file_promises).then(function() {
// all files have saved now, do other stuff here
console.log(files_array);
callback(files_array);
});
}
// sample function call
Util.upload_parse_images([{
'attr_name': 'image',
'file_obj': $("#event_image")
}, {
'attr_name': 'thumbnail',
'file_obj': $("#event_thumbnail")
}], function (images_data) {
// handling saved images data depending upon each image `attr_name`
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment