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

Blocking Methods



Non-blocking Methods:

- (void)uploadFileWithFileName:(NSString * _Nonnull)fileName filePath:(NSString * _Nonnull)filePath content:(NSData * _Nonnull)content responseHandler:^(BackendlessFile * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

- (void)uploadFileWithFileName:(NSString * _Nonnull)fileName filePath:(NSString * _Nonnull)filePath content:(NSData * _Nonnull)content overwrite:(BOOL)overwrite responseHandler:^(BackendlessFile * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func uploadFile(fileName: String, filePath: String, content: Data, responseHandler: ((BackendlessFile) -> Void)!, errorHandler: ((Fault) -> Void)!)

func uploadFile(fileName: String, filePath: String, content: Data, overwrite: Bool, responseHandler: ((BackendlessFile) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
fileName name of the file where the content should be stored.
filePath path in the remote Backendless file storage where the content should be stored.
content data to store in the file in Backendless file storage.
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.

Return Value (sync methods only)

Argument                Description
BackendlessFile a wrapper around the URL assigned to the uploaded content/file.

Example

NSData *content = [@"The quick brown fox jumps over the lazy dog" dataUsingEncoding:NSUTF8StringEncoding];
[Backendless.shared.file uploadFileWithFileName:@"fox.txt" filePath:@"MyFiles/texts" content:content overwrite:YES responseHandler:^(BackendlessFile *file) {
    NSLog(@"File has been uploaded: %@", file.fileUrl);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
if let content = "The quick brown fox jumps over the lazy dog".data(using: .utf8) {
    Backendless.shared.file.uploadFile(fileName: "fox.txt", filePath: "MyFiles/texts", content: content, overwrite: true, responseHandler: { file in
        print("File has been uploaded: \(file.fileUrl ?? "")")
    }, errorHandler: { fault in
        print("Error: \(fault.message ?? "")")
    })
}