Backendless Support
 

Uploading files to server with the File Upload API

There are several way to upload file content to the server:

  1. A traditional approach where a physical file from the client environment is uploaded using the API.
  2. Creating a remote file with content generated on the client side.

This topic covers the issue of uploading a file with the API.

Once a file is uploaded, the File Service enables the following:

  • You can see the file in File Browser in Backendless console.
  • File can be downloaded via a URL assigned to it. The URL is composed as:
<a href="<a href=" https:="" api.backendless.com="" <application"="">https://api.backendless.com/<application< a="">">https://api.backendless.com/<application">https://...</application<></a> id>/<version name="">/files/<path>/<file name=""></file></path></version>
  • Application developer can assign permissions to control who (users or roles) can download or delete the file.
  • If git integration is enabled for the application, the file is also committed to the repository.

The example below demonstrates the API for file upload:

JAVA

Asynchronous Call Example (Plain Java and Android):

private static void uploadAsync() throws Exception
{
 // create a file locally so there is something to upload
 String filename = "myhelloworld-async.txt";
 FileOutputStream fileOutputStream = new FileOutputStream( filename );
 fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
 fileOutputStream.close();
 final File file = new File( filename );
 // now upload the file
 Backendless.Files.upload( file, "/myfiles", new AsyncCallback<BackendlessFile>()
 {
 @Override
 public void handleResponse( BackendlessFile uploadedFile )
 {
 System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
 file.delete();
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
 } );
}
Synchronous Sample (Plain Java only):
private static void uploadSync() throws Exception
{
 // create a file locally so there is something to upload
 String filename = "myhelloworld-sync.txt";
 FileOutputStream fileOutputStream = new FileOutputStream( filename );
 fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
 fileOutputStream.close();
 File file = new File( filename );
 // now upload the file
 BackendlessFile uploadedFile = Backendless.Files.upload( file, "/myfiles" );
 System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
 file.delete();
}

When a developer runs the code and the files are uploaded, you can see them in Backendless console. To do that:

  1. Login to console, select an app and click the Files icon.
  2. It will be shown the “myfiles” directory created by the code above. Click the directory name to see its contents.
  3. There should appear two files as shown in the screenshot below. 
  4. To see the contents of the file click the Edit file icon: 

Is article helpful?