Twitter: raymondcamden


Address: Lafayette, LA, USA

Quick note for Parse and JavaScript Users

07-10-2013 6,941 views Mobile, JavaScript 9 Comments

Forgive the somewhat haphazard nature of this blog post. I've got a limited amount of time before I board a plane but I didn't want to wait till the morning to post. Earlier today a reader (Arcayne - sounds like an early 80s X-Man) posted a comment that led me to discover that Parse's JavaScript API now supports Files. This wasn't possible in the past. You can read the details about it here but for folks who want to see complete demo code, keep reading.

The first thing I did was build a complete demo using their example of a file upload. I began by creating a simple HTML file:

Note I've got the Parse library loaded. The HTML just contains a file field and a button. Here's the JavaScript code:

For the most part this is just what the Parse docs have, but as a complete file, hopefully it is helpful. I can confirm this worked perfectly. In the result object I got a URL to the picture I uploaded. Of course, if I did not pick an image this wouldn't work. You do have access to the file name so in theory line 14 could easily be made dynamic.

Ok, I felt bad. The fix was one line:

var name = file.name;

In the zip I'll be sharing this is corrected. So - that would work in PhoneGap if your mobile client allows for file upload fields. I'm not sure which do. I know iOS lets you capture the camera with one, but I'm not sure if I remember if it lets you select files too. As I said, I'm in a rush.

My real question though was if this could work with PhoneGap. I built a test to see if I could use the Camera API and upload it to Parse. For my first test I decided to just use Base64. That's one of the options in PhoneGap and it works with Parse as well. Turns out this worked super easy as well. I won't bother with the HTML, just the JavaScript, and I'll just focus on the important part.

Again - I assume this is all pretty simple, but if it doesn't make sense, let me know. I specified the photo library as I was testing on the iOS simulator.

Woot. So it works. But - I've heard that using base64 for image data in PhoneGap can be a bit memory intensive. Parse supports a binary array too so I built another demo that made use of "real" files as well. That turned out to be a bit more difficult.

First - PhoneGap can save a camera image to the file system. That part is easy. But the File API is not the most... simple thing to use. The PhoneGap API returns a File URI that you have to turn into a File object that you then have to send to a File Reader. Finally you can get an array buffer object but Parse needs a real array instead. (And for that part I need to thank Si Robertson for his help on G+.)

Here is the code I used. It isn't pretty. But it works.

Anyway - this is very cool and just makes the Parse/PhoneGap combination even cooler. I've attached a copy of my code with my app IDs stripped out. Enjoy!

Download attached file

Related Blog Entries

9 Comments

  • Arcayne #
    Commented on 07-11-2013 at 8:25 AM
    It works even on a early 80s X-Man phone! (30 long years waiting for such a sweet combination of PG & parse : P )

    That is exactly what I was ultimately trying to do, make it work with phonegap and parse but I had the same error (the one I've shown in the other post) when using the base64 solution and never tried the binary array option, which seems to me to be the winner of all of this solutions.

    big time thank you!
  • Arcayne #
    Commented on 07-11-2013 at 8:34 AM
    however using window.resolveLocalFileSystemURI shows this weird

    "I Haz Cheeseburger?!?!
    File.resolveLocalFileSystemURI

    We seem to be missing some stuff :(

    What is kinda cool though you can fill in the textarea to pass a json object to the callback you want to execute"

    in Ripple. Do you use it in any other way that makes it work from the laptop?
  • Commented on 07-11-2013 at 8:37 AM
    For Ripple... no. I'd use the iPhone Simulator if on Mac. Very quick.

    Side question: What version of PG are you using with your Ripple test?
  • Arcayne #
    Commented on 07-11-2013 at 8:45 AM
    2.5 in my ripple test , this uncompatibility dosen't seem to be fixed in 2.9 neither . . .

    https://groups.google.com/forum/#!topic/phonegap/o...
  • Commented on 07-11-2013 at 8:48 AM
    Heh, you read my mind.

    So as an OT FYI, I'm working with the dev on the whole Ripple/PG Recent problem. Will blog when we get it figured out.
  • Arcayne #
    Commented on 07-11-2013 at 10:58 PM
    Sweet, looking forward!
  • gary #
    Commented on 07-25-2013 at 6:34 AM
    Thanks, that was really useful
  • Ayush goel #
    Commented on 07-17-2014 at 1:49 PM
    Hey! Thanks for the great tutorial. Eveyrthing worked just fine, however when I click on the pic stored in parse database, it gives me an empty thumbnail?
    Solution for this pl!
  • Commented on 07-19-2014 at 6:20 PM
    I don't know - I'd need to run your app to diagnose more. I'd suggest seeing if anything shows up in the console.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty