Over a million developers have joined DZone.

How to Upload Files From a Mobile App to a Cloud Database

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

Mobile developers often face the need to upload binary files, such as images, to the database. For example, this could happen in an app like Instagram, where a user takes photos with the device’s camera and then publishes it to their followers. Of course, all the data — including images — should be stored in the database, so the user can sign in from any device and see his or or her feed.

Another possible case for uploading images is sending an MMS (multimedia message) from your app, which can be done via the Twilio API. Twilio accepts a direct URL to an image. A user takes a picture with the device and uploads it to the Appery.io database. Then, a direct image URL is passed to the Twilio API.

In Appery.io, there are three ways to upload files to the database :

  • Via Web API (this approach uses only the browser API).
  • Via Cordova file-transfer.
  • Via the Input component and JavaScript helper (It’s also possible to upload multiple files via the Input component, see this tutorial for more).

We’ve created a plug-in for the first two of these, and, with it, you can quickly implement file uploading in your mobile Appery.io app. It consists of two pages – cordovaupload and webuploadThe cordovaupload uses the Cordova file-transfer plug-in. To make it work, the app has to run on the device or via the Appery.io Tester app.

The webupload page uses the browser FormData API to upload a file. This approach can be used to test the app in the browser, which is faster than on the device without having to take a picture each time. The Web API can be also used to upload images taken via the camera on the device.

A quick note about configuring the plugin: the Appery.io database requires a sessionTokenfor uploading files to the database. To make the plugin simpler to use, it uses the Master Key. So, to configure this plugin, you should provide the ID of the database that will be used to store files and the Master Key instead of sessionToken.

However, please keep in mind that for security reasons, we recommend you use sessionToken instead of Master Key. You can learn how to implement user authentication and registration with Appery.io here.

This plug-in is configured out-of-the-box to work with the Appery.io database, but it can be modified to work with any other API provider that allows file upload. You will need to change the upload REST API and update any parameters.

As usual, we’ve prepared a detailed tutorial for this plug-in, which you can find here.

If you have any problems with this plug-in or with file uploading in Appery.io, contact our support and they will help you right away.

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Max Katz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}