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.

Methods

Blocking API

public String SaveFile( String filePathName, byte[] fileContent )
public String SaveFile( String filePathName, byte[] fileContent, Boolean overwrite )
public String SaveFile( String path, String fileName, byte[] fileContent )
public String SaveFile( String path, String fileName, byte[] fileContent, Boolean overwrite )

Non-Blocking API

public void SaveFile( String filePathName, byte[] fileContent, AsyncCallback<String> responder )
public void SaveFile( String filePathName, byte[] fileContent, Boolean overwrite, AsyncCallback<String> responder )
public void SaveFile( String path, String fileName, byte[] fileContent, AsyncCallback<String> responder )
public void SaveFile( String path, String fileName, byte[] fileContent, Boolean overwrite, AsyncCallback<String> responder )

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.

Return Value (delivered via AsyncCallback)

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

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)
static void SaveFile()
{
   String content = "The quick brown fox jumps over the lazy dog";
   byte[] bytes = UTF8Encoding.UTF8.GetBytes( content );
   String savedFileURL = Backendless.Files.SaveFile( "tempfolder", "fox.txt", bytes, true );
   System.Console.WriteLine( "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.

Codeless Reference

files_api_create_file_1

where:

Argument                Description
path Path of the directory where the file should be stored. Must start with "/" which represents the root directory of the remote file storage.
file name Name of the file where the byte content should be written to.
file content An array of bytes to save as text.
overwrite file 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.
return URL of the created file When this box is checked, the successful operation returns a URL to the created file.

Returns a URL to the created file.

The example below creates a new file named "notes.txt" in the "/misc" folder. The contents of the file is text converted from the bytes array (base 64 encoded). The decoded value is "my cool note".

files_api_save_bytes_array_to_file