simple online nosql database database with REST API and MongoDB queries
  • Features
  • Docs
  • Blog
  • Templates
  • Pricing
  • Contact
  • Sign Up
  • Log In

Blog

 The latest news and articles about restdb.io  

Database templates!
Don't forget to visit our template catalog! It's the fastest way to get up and running with a restdb.io database. View templates »

A picture is worth a 1000 lines of code

by Jon Erik Solheim
|
Howto|API|
  • Share on Facebook
  • Tweet
  • Add to Pocket
  • Share on LinkedIn
  • Send email
restdb.io comes with a helpful media archive for documents and images. The media archive also has an API with full CORS support. This is really handy for providing your web application with dynamic content.

In this blog post we'll look at the media archive API and go through a JavaScript code example for a simple (ugly) web page showing the basics of the API.
Adding support for media files is important in many applications. For example: a job application form with support for attaching a CV, or a profile page where the user can add her own profile picture, just to mention a few use cases.

restdb.io makes these scenarios simple, read on to learn how.

In this example we'll be using the demo database:

https://rdb-simpledb.restdb.io

Media archive API

Before we start to code, lets take a brief look at the API. It's really simple, only 4 endpoints:
Verb Resource Description
GET https://[database]/media/[ID] Gets the binary data for resource with ID.
https://rdb-simpledb.restdb.io/media/56d5ac3dec65af7000000178
GET https://[database]/media/meta[ID] Gets the meta data for resource with ID.
https://rdb-simpledb.restdb.io/media/56d5ac3dec65af7000000178/meta
POST https://[database]/media/[ID] Post a new media object to the database, returns ID.
https://rdb-simpledb.restdb.io/media
DELETE https://[database]/media/[ID] Delete media object with ID.
https://rdb-simpledb.restdb.io/media/56d5ac3dec65af7000000178
In our example we'll be using HTML5 and JQuery to build a web page that uploads files to the media archive and displays meta data for each file. The finished page looks something like this example: 
 
Before we begin, remember to set up your API key with CORS support. You must use a valid key that allows both POST and DELETE for this example.

var apikey = "569a2c875667r9cf4b98qa50";
$.ajaxPrefilter(function( options ) {
	if ( !options.beforeSend) {
		options.beforeSend = function (xhr) { 
			xhr.setRequestHeader('x-apikey', apikey);
		}
	}
});

POST a new image/file to the database

Now that our CORS API key is set up, we can move on to the HTML markup code for uploading files to the database. The HTML is just one input with type=file and a button that calls our Javascript when clicked.

<p><input type="file" name="file1" id="file1">	
<p><button id="uploadButton" type="button" onclick="ff()">Upload</button>
We use Ajax to post files, and to start that process we need to create a FormData object with the selected files.

var formData = new FormData();
var files = $('#file1')[0].files; 
// Loop through each of the selected files.
for (var i = 0; i < files.length; i++) {
	var file = files[i];
	var name = files[0].name;
	// Add the file to the request.
	formData.append('myfile', file, file.name);
}
The FormData object now contains the selected files ready to be uploaded. The code to upload files is straightforward.

$.ajax({
	"data": formData,
	"url": "https://rdb-simpledb.restdb.io/media",
	"method": "POST",
	"contentType": false,
}).done(function (response) {
	...
});
If the upload is a success, the returning data will contain a reference for the upload bulk and an array with the database ids for each file.

{
  "msg": "OK",
  "uploadid": "6772edc9cfcf010d04977a63b9bc3eb8",
  "ids": [
    "570e052d98290e490000006e"
  ]
}

GET an image/file from the database

To display the uploaded image, we just grab the id and generate an img tag with the correct source. In this example we use a little trick, using the onerror event to insert an alternative image for files that aren't an image.

<img src="https://rdb-simpledb.restdb.io/media/570e052d98290e490000006e" onerror="this.src='... some url ...'">

GET image/file meta data

In addition to uploading and storing the file, restdb.io also extracts all metadata from the uploaded file object and stores it in the media archive. To retrieve the metadata for an image, we use the ID to call the API.

$.getJSON("https://rdb-simpledb.restdb.io/media/570e052d98290e490000006e/meta", function(metadata){
	...
});
The return object contains all data about the media resource. The exif property contains additional meta data associated with the particular file type.

{
    "_id": "570e0a1798290e4900000071",
    "uploadid": "e45ad57eddffa14f5e8cb76caefa7481",
    "_created": "2016-04-13T08:57:59.821Z",
    "_createdby": "api",
    "app": "rdb-simpledb",
    "origname": "AutoWallpaper_ 300150.jpg",
    "file": "9b79501dbd42b59b12d737b4f66fe0f7AutoWallpaper_ 300150.jpg",
    "fullname": "/rdb-simpledb/mediaarchive/9b79501dbd42b59b12d737b4f66fe0f7AutoWallpaper_ 300150.jpg",
    "exif": {
      ...
    },
    "owner": "rdb-simpledb",
    "mediatype": "image"
  }
A typical exif meta data example for a JPEG image looks like:
Lamborghini-Cars-HD-Wallpaper.jpg (image)
exiftool version number9.46
file name18e04fdcefca5b01b85d2181d596c1f8Lamborghini-Cars-HD-Wallpaper.jpg
file size246 kB
file modification date time2016:04:12 15:45:55-04:00
file access date time2016:04:12 15:45:55-04:00
file inode change date time2016:04:12 15:45:55-04:00
file permissionsrw-r--r--
file typeJPEG
mime typeimage/jpeg
jfif version1.01
resolution unitinches
x resolution96
y resolution96
exif byte orderLittle-endian (Intel, II)
warningInvalid EXIF text encoding
user commentCREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 100.
image width1920
image height1080
encoding processBaseline DCT, Huffman coding
bits per sample8
color components3
y cb cr sub samplingYCbCr4:2:0 (2 2)
image size1920x1080

DELETE image/file from the database

To delete a file from the media archive, we use the ID for the file to be deleted and call the API.

$.ajax({
	"url": 'https://rdb-simpledb.restdb.io/media/570e0a1798290e4900000071',
	"method": 'DELETE'
})
.done(function(resp){
	...
})
.fail(function(error){
	...
});	
We hope this example has shown you how easy it is to integrate dynamic media content in your application.


Full source code at Github here.
  • Share on Facebook
  • Tweet
  • Add to Pocket
  • Share on LinkedIn
  • Send email

All Posts


Search

Topics

  • API
  • Authentication
  • Case study
  • Features
  • Howto
  • Integrations
  • Newsletter
  • Productivity
  • Prototyping
  • Tutorials
  • User

restdb.io is a fast and simple NoSQL cloud database service. With restdb.io you get schema, relations, REST API and an efficient multi-user admin UI for working with data.

Our customers use it for backends, business databases, API-first CMS, data collection and much more. It is easy to get started with the free development plan.

Start Building »

  • Site Links
  • Home Page
  • About Us
  • Contact
  • Blog
  • Templates Catalog
  • Cloud Service
  • Features
  • Pricing
  • Terms & Conditions
  • Privacy Policy
  • Sign Up »
  • Documentation
  • Overview
  • Getting Started
  • Coding against the API
  • Utils
  • Security and Admin
© 2025 restdb.io