Skip to content

File Upload

The file upload operation delivers and saves a local file in the remote Backendless file storage. The return value of the operation is the file URL which has the following structure:

https://backendlessappcontent.com/<application id>/<REST-API-key>/files/<path>/<file name>

where:

Argument                Description
<application id> ID of the application which can be obtained from the Manage > App Settings screen of the Backendless Console
<REST-API-key> REST API key of your application. You can get the value from the Manage > App Settings section of the Backendless Console. Note that if the file is created from Cloud Code (JS, Java or Codeless), the API key in the file path will be the CodeRunner API Key.
<path> directory path where the file is saved
<file name> name of the file

The URL assigned to a file and returned as a result of the upload operation accounts for any security permissions assigned to the file (or the folder it is located in).

// single file upload
Backendless.Files.upload( file, path, overwrite )
 .then( function( fileURL ) {
  })
 .catch( function( error ) {
  });

where:

Argument                Description
file an instance of JavaScript File class.
overwrite a boolean value indicating whether the upload must overwrite an existing file.
path directory path (without the name of the file) in the Backendless file storage where the file should be stored. If the path does not exist, Backendless File Service creates the directory structure.

Return Value

URL of the uploaded file.

Example

HTML:

<input type="file" id="files" name="files[]" multiple />
<input type="button" onclick="uploadFileFunc(); return false;" value="Upload File"/>
Javascript:
// this line goes into the app initialization block
document.getElementById('files').addEventListener('change', handleFileSelect, false);

function handleFileSelect(evt) 
{
   file = evt.target.files[0]; // FileList object
}

function uploadFileFunc()
{
  var callback = {};

   callback.success = function(result)
   {

   }

   callback.fault = function(result)
   {
       alert( "error - " + result.message );
   }

   Backendless.Files.upload( file, "my-folder" )
    .then( function( fileURLs ) {
       console.log( "File successfully uploaded. Path to download: " + result.fileURL );
     })
    .catch( function( error ) {
       console.log( "error - " + error.message );
     });