Contents
What are we going to build?
In this tutorial we are going to build a mobile app that uploads images to Tiggzi database.
The app is going to be HTML5 app that uses browsers File API (different from Tiggzi Database File API). File API is supported in most desktop browsers. However, if you want to try this app in a mobile browser, you would need to install Firefox Mobile or Opera Mobile browser. Both have support for the File API. More information on which browsers support the File API.
Before you start
- Tutorial level: Advanced
- Prerequisites: Completed at least 1 beginner and 1 other advanced tutorial.
Creating new app
Let’s start by creating a new app.
- From Apps page, for app name enter UploadingImagesApp and click Create.
- A new app will be created, and the mobile builder will be loaded.
- When the mobile builder loads, you should see something like this:
- On the left-hand side click Pages > startSceen - editor with an empty startScreen will open:
Building the Sign In page
File upload functionality requires a user to be signed in. User managment feature is built into Tiggzi database, so it’s pretty simple to implement. Rename the default first page to Login and then build the following page:
- The input fields are named: username and password.
- Name and Password are both placeholders.
- The Password field, its type is set to password.
Creating a new database and user
- To create a new database, go to http://io.tiggzi.com or click the Backend Services button
- Enter FileDB for database name and click Create. You will see the database Dashboard:
- Next, open Users tab
- Click New (on the left-hand side) to create a new user.
- Enter the username and password:
- Click Add to save this user.
You can create as many users as you want.
It’s also possible to create users via REST API, but in this tutorial we are manually entering a user directly into the database.
Signing in the user into the app
Creating Sign In REST service
Now that we got a Sign In, a user in the database, let’s work on creating the actual service to sign in the user.
You can always create a new REST service by going to Create New > Service and then entering all the information such as URL, and request parameters. There is a new and faster way to do the same with database integration in the app builder.
- Click Create New > Database Services
- Select FileDB database:
- We only need Login, so check that service.
- Click Import selected services button.
Under Services, the FileDB_login_service will be created (plus a settings file that has the database ID in it). Next we need to add the service to the page and map it to the page. If you open the service, you will see that the URL, request parameters and response parameters are all defined.
Mapping the service to the page
- Let’s first add the service to the page. To add a service instance to the page, go to Data tab > from list select Service > FileDB_login_service, click Add button.
- Let’s also rename the service instance, set its name to signin.
- To map the service to the page, click Edit Mapping button.
- For Request Mapping, map the components as follows:
- Switch to Response Mapping.
- We want to save the sessionToken in local storage. On the right-hand side, in the input at the top enter: token and click Create > Local storage variable. The variable will be shown under Local Storage.
- Create the following mapping:
We save user’s sessionToken in local storage so that we can use later.
We now want to invoke the service to sign in the user and navigate to a page where we can upload an image. The second page doesn’t exist yet so let’s create it.
- In Project view, Create New > Page.
- Call the screen Upload. We will come to creating the UI in the next step.
Invoking the service
- Open the Login page.
- We want to invoke the service on button click. Go to Design tab, open Events view (on the bottom of the screen).
- Select the button > Click event > Invoke Service > signin and click Add event button.
One we successfully sign in, we want to navigate to Upload page.
- Go back to Data tab, open Events view.
- Select signin service instance > Success > Navigate To Page > Upload page.
- Click Add event.
Let’s also add some basic error checking in case the sign in failed.
- For signin service instance also add Error event
- Then action Run JavaScript and enter:
alert ("Signin failed");
- Click Add event to save.
We are done here. Go ahead and test the app.
Next we are going to work on Upload page.
Building the Upload page
Open the Upload page and build the following UI:
- The Select File to Upload button is outside the grid.
- Image, Label and Upload File button are inside the grid
- Set the following component names:
- Name the grid: preview
- Name the image: image
- Name the label: fileName
- Outside the grid, there is a Panel component for writing any custom code.
We want to hide the preview (the grid) area before the file was selected. Select the grid (preview) and in properties uncheck Visible. Use the breadcrumbs at the top to make it easier to select the gird. The grid will become grayed out:
Let’s now add code to show a file selection window.
Invoking file selection window
- Select the Panel component
- In Properties, set its type to html
- Click Edit HTML and enter the following code:
<form enctype="multipart/form-data" method="post" name="fileinfo" style="visibility:hidden">
<fieldset>
<input type ="file" name ="fileselect" id="fileselect"></input>
</fieldset>
</form>
<script type="text/javascript">
// set event listener for call preview after select file
var fileselect = $('#fileselect');
fileselect.bind("change", fileSelectHandler);
</script>
Click Select File to Upload button and add click event with Run JavaScript action. Enter the following code:
$('#fileselect').trigger('click');
Click Add event.
Uploading files to Tiggzi database
The uploading of the actual file will be done in a custom JavaScript file. Create a new JavaScript file (Create New > JavaScript), name it Upload and enter the following code:
var file;
function fileSelectHandler(e) {
var files = e.target.files || e.dataTransfer.files;
file = files[0];
previewFile();
}
function upload() {
var serverUrl = 'https://api.tiggzi.com/rest/1/db/files/' + file.name;
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Tiggzi-Database-Id", FileDB_settings['database_id']),
request.setRequestHeader("X-Tiggzi-Session-Token", localStorage.getItem('token')),
request.setRequestHeader("Content-Type", file.type);
},
url: serverUrl,
data: file,
processData: false,
contentType: false,
success: function(data) {
// OPTIONAL, this is the file name under which the image was stored in database....
// localStorage.setItem('db_file_name', file.name);
},
error: function(data) {
// do something in case of an error...
}
});
}
function previewFile() {
var previewContainer = $('table[dsid=preview]');
var fileName = $('[name=fileName]');
// make the preview container visible once a file was selected
previewContainer.toggle();
// set the file name
fileName.text(file.name);
// display image in preview container
if (file.type.indexOf("image") == 0) {
var reader = new FileReader();
reader.onload = function(e) {
var image = $('[name=image]');
image.attr('src', e.target.result);
$('[class=mobileimage1_div]').show();
}
reader.readAsDataURL(file);
}
}
The very last thing we need to do is to invoke this code when the upload button is clicked.
- Go back to Upload page, open Events tab.
- Select Upload File button > Click > Run JavaScript and enter the following:
upload();
- Click Add event.
You are ready to test the app.
Testing the app
When you click Upload File, you can go to Files tab in the database to see the uploaded file:
Viewing the uploaded file
To view the file, you would need to create a service with the following URL (File API):
https://api.tiggzi.com/rest/1/db/files/_image_name_from_db
For example, using the just uploaded file, the URL would look like this:
https://api.tiggzi.com/rest/1/db/files/3bce250b-aadf-4042-9728-f93adf5a2abd.selection.png
For Request Parameters, you need the following parameters: