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.

You can save a file with or without overwriting the existing file. To specify this, use overwrite argument.

Method

PUT

URL

https://api.backendless.com/<application-id>/<REST-api-key>/files/binary/<filePath>?overwrite=true

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.
<filePath> path to a directory where the file will be stored located.
overwrite defines whether a newly saved file overwrites the existing file. If the argument value is true, the new file overwrites the existing one. Otherwise, if the value is false or the overwrite parameter is not present, and another file with the same name already exists, the server returns an error.

Request headers

Content-Type: multipart/form-data  
user-token: optional value obtained as a result of the login operation.

where:

Argument                Description
Content-Type Must be set to multipart/form-data. This header is mandatory.
user-token Optional header. Contains a value returned by Backendless in a preceding user Login API call. If user-token is set in the request, the currently logged in user will be assigned to the ownerId property of the user object which is being saved.  Additionally, the operation will be executed with the security policy associated currently logged in user. This means all permissions associated with the user and the roles assigned to the user will be enforced by Backendless.

Request Body:

A byte array encoded as a Base64 string.

Response Body

URL of the newly saved file or an error.

Example

The example below saves the "savedNote.txt" file with the content "my cool note". The content must be encoded in the Base64 format, hence the value is "bXkgY29vbCBub3Rl"

curl -H Content-Type: text/plain -X PUT \  
-v https://api.backendless.com/XXXX-XXXX-XXXX/ZZZZ-ZZZZ-ZZZZ/files/binary/testFolder/savedNote.txt?overwrite=true" \  
-d bXkgY29vbCBub3Rl

where

Argument                Description
-d the request body
bXkgY29vbCBub3Rl a byte array (string) encoded as a Base64 string. The decoded value is "my cool note".

The server will return the URL of the newly saved file:

"https://api.backendless.com/XXXX-XXXX-XXXX/ZZZZ-ZZZZ-ZZZZ/files/testFolder%2FsavedNote.txt"

If you opt not to overwrite the existing file,the overwrite argument may be skipped or set to false. In that case, if the file already exists, a new file is not saved and server will return an error. If the file does not exist, the server will return the URL of the newly saved file.

Errors

When the server-side reports an error, it returns a JSON object in the following format:

{  
  "message":error-message,  
  "code":error-code  
}

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.