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://api.backendless.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.
<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).

Java and Android

Non-Blocking API

Upload a file to a remote path in the Backendless storage.

// uploads a file to a remote directory in Backendless Hosting
public void Backendless.Files.upload( File file,
                                      String remotePath,
                                      AsyncCallback<BackendlessFile> responder )

// same as the method above, but with an option to overwrite a file if one exists with the same name
public void Backendless.Files.upload( File file,
                                      String remotePath,
                                      boolean overwrite,
                                      AsyncCallback<BackendlessFile> responder )

Upload a file to a remote path and receive updates for the status of the upload. Updates are represented as percentages and are delivered via the uploadCallback argument.

// uploads a file to a remote directory in Backendless Hosting.
public void Backendless.Files.upload( File file,
                                      String remotePath,
                                      UploadCallback uploadCallback,
                                      AsyncCallback<BackendlessFile> responder )

// same as the method above, but with an option to overwrite a file if one exists with the same name
public void Backendless.Files.upload( File file,
                                      String remotePath,
                                      boolean overwrite,
                                      UploadCallback uploadCallback,
                                      AsyncCallback<BackendlessFile> responder )

Blocking API

Upload a file to a remote path in the Backendless storage.

// uploads file to the specified path in the Backendless Hosting storage
public BackendlessFile Backendless.Files.upload( File file, 
                                                 String remotePath ) 
                                                          throws Exception

// same as the above, but provides an option to overwrite if 
// another file already exists with the same name
public BackendlessFile Backendless.Files.upload( File file, 
                                                 String remotePath, 
                                                 boolean overwrite ) 
                                                           throws Exception

Upload a file to a remote path and receive updates for the status of the upload. Updates are represented as percentages and are delivered via the uploadCallback argument.

// uploads file to a directory in Backendless Hosting. 
public BackendlessFile Backendless.Files.upload( File file,
                                                 String remotePath,
                                                 UploadCallback uploadCallback ) 
                                                          throws Exception

// same as the method above, but with an option to overwrite 
// the file if one exists with the same name
public BackendlessFile Backendless.Files.upload( File file,
                                                 String remotePath,
                                                 boolean overwrite,
                                                 UploadCallback uploadCallback ) 
                                                           throws Exception

Create a file in the remote Backendless file storage from the bytes provided by OutputStream.

// uploads data from output stream provided by the outputStreamRouter object to a remote
// directory in Backendless Hosting.
public BackendlessFile Backendless.Files.uploadFromStream( 
                                             IOutputStreamRouter outputStreamRouter,
                                             String remoteName,
                                             String remotePath ) throws Exception

// same as the method above, but with an option to overwrite 
// the file if one exists with the same name
public BackendlessFile Backendless.Files.uploadFromStream( 
                                             IOutputStreamRouter outputStreamRouter,
                                             String remoteName,
                                             String remotePath,
                                             boolean overwrite ) throws Exception

where:

Argument                Description
file local file to upload to Backendless file storage. Remote file name is the same as the name of the local file.
remotePath path (without file name) in the remote Backendless file storage where the file should be saved.
uploadCallback receives updates through the upload process. Updates expressed as percentage of file uploaded to the server.
outputStreamRouter a wrapper around a java.io.ObjectStream object which provides the bytes for the upload.
remoteName name of the file to save in the remote Backendless file storage.
responder a responder object which receives a callback when the method successfully uploads the file or if an error occurs. Applies  to the asynchronous methods only.

Android Only

Blocking API

Upload a bitmap image and save as a file in the remote Backendless storage.

// uploads an android bitmap to a remote directory in Backendless Hosting
public BackendlessFile Backendless.Files.upload( 
                               android.graphics.Bitmap bitmap,
                               android.graphics.Bitmap.CompressFormat compressFormat,
                               int quality,
                               String remoteName,
                               String remotePath ) throws Exception

// same as the method above, but with an option to overwrite 
// the file if one exists with the same name
public BackendlessFile Backendless.Files.upload( 
                               android.graphics.Bitmap bitmap,
                               android.graphics.Bitmap.CompressFormat compressFormat,
                               int quality,
                               String remoteName,
                               String remotePath,
                               boolean overwrite ) throws Exception

Non-Blocking API

Same as the method above, but the upload is handled asynchronously.

// uploads a bitmap and stores it in a file in remote directory in Backendless Hosting
public void Backendless.Files.upload( 
                          android.graphics.Bitmap bitmap,
                          android.graphics.Bitmap.CompressFormat compressFormat,
                          int quality,
                          String remoteName,
                          String remotePath,
                          AsyncCallback<BackendlessFile> responder ) throws Exception

// same as the method above, but with an option to overwrite 
// the file if one exists with the same name
public void Backendless.Files.upload( 
                          android.graphics.Bitmap bitmap,
                          android.graphics.Bitmap.CompressFormat compressFormat,
                          int quality,
                          String remoteName,
                          String remotePath,
                          AsyncCallback<BackendlessFile> responder ) throws Exception

where:

Argument                Description
bitmap bitmap to upload to the remote Backendless storage and save as a file.
compressFormat compress format to use when compressing the bitmap. The compression is done by the Bitmap.compress method.
quality quality of the compression. The value is passed to the Bitmap.compress method, which handles the compression.
remoteName name of the file to save in the remote Backendless file storage.
remotePath path in the remote Backendless file storage where the file should be saved.
responder a responder object which receives a callback when the method successfully uploads the file or if an error occurs. Applies  to the asynchronous methods only

Return Value (sync methods only)

Argument                Description
BackendlessFile a wrapper around the URL assigned to the uploaded file.

Example

Bitmap photo = (Bitmap) getIntent().getExtras().get( "data" );

Backendless.Files.Android.upload( photo, 
                                  Bitmap.CompressFormat.PNG, 
                                  100, 
                                  "myphoto.png", 
                                  "mypics", 
                                  new AsyncCallback<BackendlessFile>()
{
 @Override
 public void handleResponse( final BackendlessFile backendlessFile )
 {
 }

 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
   Toast.makeText( UploadingActivity.this, 
                   backendlessFault.toString(), 
                   Toast.LENGTH_SHORT ).show();
 }
}