Skip to content

Save Files From Byte Arrays

In addition to the classic file upload, files can be saved by uploading a byte array which becomes the content of the saved file.

Non-Blocking API

// Save file in a remote path in the Backendless file storage. The path includes both the directory 
// where the file should be saved and the file name. If the file with the same name already exists, 
// it is not overwritten and error is returned.
public void saveFile( String filePathName, byte[] fileContent, 
                                                 AsyncCallback<String> responder )

// Save a file in a remote path in the Backendless file storage. The path includes both the directory
// where the file should be saved and the file name. The overwrite argument determines if the file 
// should be overwritten if it already exists.
public void saveFile( String filePathName, byte[] fileContent, boolean overwrite, 
                                                 AsyncCallback<String> responder )

// Save a file in a remote path in the Backendless file storage. If the file with the same name 
// already exists, it is not overwritten and error is returned.
public void saveFile( String path, String fileName, byte[] fileContent, 
                                                 AsyncCallback<String> responder )

// Save a file in a remote path in the Backendless file storage. The overwrite argument determines 
// if the file should be overwritten if it already exists.
public void saveFile( String path, String fileName, byte[] fileContent, boolean overwrite, 
                                                 AsyncCallback<String> responder )

Blocking API

// Save a file in a remote path in the Backendless file storage. The path includes 
// both the directory where the file should be saved and the file name. If another 
// file with the same name already exists, it is not overwritten and an error is returned.
public String saveFile( String filePathName, byte[] fileContent )

// Save a file in a remote path in the Backendless file storage. The path includes 
// both the directory where the file should be saved and the file name. The 
// overwrite argument determines if the file should be overwritten, if it already exists.
public String saveFile( String filePathName, byte[] fileContent, boolean overwrite )

// Save a file in a remote path in the Backendless file storage. If the file with 
// the same name already exists, it is not overwritten and error is returned.
public String saveFile( String path, String fileName, byte[] fileContent )

// Save a file in a remote path in the Backendless file storage. The overwrite 
// argument determines if the file should be overwritten if it already exists.
public String saveFile( String path, String fileName, byte[] fileContent, boolean overwrite )

where:

Argument                Description
filePathName identifies both the directory where the file should be saved and the file name. Must start with "/" which represents the root directory of the remote file storage.
path path of the directory where the file should be stored. Must start with "/" which represents the root directory of the remote file storage.
fileName name of the file where the byte content should be written to.
fileContent an array of bytes to save.
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.
responder a responder object which receives a callback when the method successfully completes or if an error occurs. Applies to the asynchronous methods only.

Example

The example below describes how to save a file called "fox.txt" from the string "The quick brown fox jumps over the lazy dog." You will need to specify:

  • content of a new file ("The quick brown fox jumps over the lazy dog")
  • where to save the new file ("testfolder")
  • a name of the newly created file ("fox.txt")
  • whether a new file should overwrite the existing file, if any (true)
public static void saveFile() 
{ 
  byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(); 
  String savedFileURL = Backendless.Files.saveFile( "tempfolder", 
                                                    "fox.txt", 
                                                    bytes, 
                                                    true ); 
  System.out.println( "File saved. File URL - " + savedFileURL ); 
}

The server will return notification and link to the newly added file ("File saved. File URL - ") or an error.

Errors

Error codes returned on attempt to save a file from the byte array.

Error Code
Description
6016
When saving a new file from the byte array, the payload exceeds 2,800,000 bytes.
6003
A file you are trying to save already exists in the system and cannot overwrite since overwrite argument is ether set to false or omitted.