Uploading Files

Uploading Files

The upload operation allows you to upload files from the user device to Telerik Platform. How to implement it depends on what development framework that you use.

Upload Files in Cordova Hybrid Apps

The Files.upload method wraps the Apache Cordova upload() FileTransfer API function. It allows you to upload files to Telerik Platform. To use it, ensure that the File Transfer plugin is enabled in your Apache Cordova project.

var fileToUpload = 'cdvfile://localhost/persistent/path/to/folder/list.txt';
var options = {
    fileName: 'list.txt',
    mimeType: 'text/plain'
};

el.files.upload(fileToUpload, options)
    .then(function() {
        // the file has been uploaded to the target location
    }, function(err) {
        // handle any errors that might have occurred during upload
    });

For details on using the cdvfile protocol for specifying file paths, see the Cordova File Plugin documentation.

Although options is an optional parameter, not specifying fileName and mimeType means saving the file with the Apache Cordova defaults which are image.jpg and image/jpeg.

If you have restricted anonymous upload of files, or if you want to authorize the current user, you need to send the access_token for the user. One way to obtain it is to call the Everlive instance's buildAuthHeader() method to get the authorization header:

options.headers = el.buildAuthHeader();

Upload Files in BASE64 Format

If your file is already in base64 format, you may find it easier to upload it to Telerik Platform using the Files.create() JavaScript SDK function. It always transfers file content in base64 and supports all supported development frameworks, including NativeScript, Node.js, Cordova, and Web.

var el = new Everlive('your-app-id');

var file = {
    "Filename": "everlive.png",
    "ContentType": "image/png",
    "CustomField": "customValue",
    "base64": /* the file contents in base64 format */
};

el.files.create(file,
    function (data) {
        alert(JSON.stringify(data));
    },
    function (error) {
        alert(JSON.stringify(error));
    });

The maximum upload size when using base64 is around 3MB. This restriction is a consequence of the maximum request body size imposed by Telerik Platform when using JSON, which is set at 4MB. When you take into account the overhead of the base64 encoding which increases the payload by about 4/3, and you get the ~3MB file upload limit.

Upload Files Using an HTML Form

In a mobile Web application the browser would not support file system operations such as reading. The solution is using the HTML form tag.

<script>
    var el = new Everlive('your-app-id');
    window.onload = function () {
        document.getElementById('form1').action = el.files.getUploadUrl();            
    };
</script>

<form id="form1" method="post" enctype="multipart/form-data">
    <input type="file" name="file1" />
    <input type="file" name="file2" />
    <input type="submit" value="submit" />
</form>

In the example above, the getUploadUrl() method returns a URL that can be used as an endpoint for uploading files. It is specific for each Telerik Platform app.

The JavaScript code sets the form tag's action attribute to the URL returned by getUploadUrl(). The form's method attribute is set to POST and its enctype attribute is set to multipart/form-data. The input tags of type file must have their name attributes set.

If you have restricted anonymous upload of files, you need to send the access_token for the user as a URL parameter. One way to obtain it is to call the Everlive instance's buildAuthHeader() method to get the authorization header.

<script>
    var userToken = 'access-token-here';
    var urlParams = '?access_token=' + userToken;
    document.getElementById('form1').action = el.files.getUploadUrl() + urlParams;
</script>

Upload Timeout

All upload requests that do not complete within 10 minutes are timed out and canceled.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.