Lepozepo / cloudinary

MIT License
94 stars 42 forks source link

Is there a way to do synchronous uploads? #52

Closed petestreet closed 8 years ago

petestreet commented 8 years ago

Following the readme instructions I'm able to upload a photo, and inside the (err, res) callback I get the resulting object. This is great, however I would really like to be able to call another function once that callback has returned. Code directly inside the (err, res) callback will be executed once it returns, but saying, for example, if (err) or if (res) inside that callback is executed immediately, thus skipped over.

The motivation behind this is I want to allow the user to upload potentially multiple images and append all of those images' public_ids to a document before inserting it into the database. But as-is I can't check whether the callback is successful in an if-block for a photo upload, and thus I can't upload multiple photos in succession (calling Cloudinary.upload() each time) and know that I'll be inserting the document only after all the uploads have completed and returned.

I thought there would be jQuery method to solve this, but haven't found anything that works. Thanks!

Lepozepo commented 8 years ago

Hmm, all public_ids are stored in the client-side Cloudinary.collection object which you can access at any time after files have been uploaded. You can also use the multiple attribute on your input to accept multiple files in one hit. This I'm sure you already know though.

I would definitely not recommend keeping that data stored temporarily in the front for too long. There are a lot of things that can happen on the client side that are out of your control and can mess up your UX. I would store the data immediately and use a separate collection to map the information. This will make your data easier to manage and improve your UX greatly since the information will persist across harsh conditions.

Still, if this doesn't make sense for your particular problem, then maybe I'm not understanding it.

petestreet commented 8 years ago

Honestly I hadn't thought of the multiple attribute beforehand, I'm facepalming right now. I think that'll be fine for my purposes - essentially the goal was to let users add one or multiple files to a "post" they make. When I read in the files array, I added a little hack to increment the 'index' of the file array every time Cloudinary.upload() is called, and only insert the post into the database once that index reaches the length of files.

I tested navigating away from the page while an upload of multiple images was in progress, and it still completed all of the uploads. Meteor magic, I suppose.

Thanks for the help, and for writing this plugin!