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).

Method

// uploads a file to a remote directory in Backendless Hosting  with an options to overwrite a file if one exists with the same name and to receive updates for the status of the upload
Future<String> Backendless.files.upload(File file, String path, {bool overwrite, void onProgressUpdate(int progress)});

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.
path path in the remote Backendless file storage where the file should be saved.
overwrite the file is overwritten if the argument value is true and the file already exists. Otherwise, if the value is false and another file with the same name already exists, an error  is returned.
onProgressUpdate receives updates through the upload process. Updates expressed as percentage of file uploaded to the server.

Example

String filename = "myhelloworld-async.txt";
File file = await File(filename).writeAsString("Hello mbaas!\nUploading files is easy!");

Backendless.files.upload(file, "/myfiles").then((response) {
  print("File has been uploaded. File URL is - " + response);
  file.delete();
});