How to download files from Backendless File Hosting

by on April 20, 2015

We’ve written about how to upload files to the Backendless Hosting system. Once a file is uploaded, it gets a public URL which can either be obtained using Backendless Console or calculated using the following template:<application id>/<version name>/files/<path>/<file name>

Alternatively, when a file is uploaded, the API call returns the URL of the uploaded file. The sample code below demonstrates how to download the file. The code prints out the contents of the file to the system console, but it can be easily modified to store it in the local file system, or transfer elsewhere over the network, etc.

    public static void downloadFile(BackendlessFile backendlessFile) throws IOException {
       URL url = new URL(backendlessFile.getFileURL());
       HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
       int responseCode = httpConn.getResponseCode();
       // always check HTTP response code first
       if (responseCode == HttpURLConnection.HTTP_OK) {
           // opens input stream from the HTTP connection
           InputStream inputStream = httpConn.getInputStream();
           InputStreamReader inputStreamReader = new InputStreamReader((inputStream));
           BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
           Log.i(TAG, "File content is:\n===========================");
           String line;
           while ((line = bufferedReader.readLine()) != null) {
               Log.i(TAG, line);
           Log.i(TAG, "===========================");
           Log.i(TAG, "File downloaded");
       } else {
           Log.i(TAG, "No file to download. Server replied HTTP code: " + responseCode);

    fun downloadFile(backendlessFile: BackendlessFile) {
       val url = URL(backendlessFile.fileURL)
       val httpConn = url.openConnection() as HttpURLConnection
       val responseCode = httpConn.responseCode
       // always check HTTP response code first
       if (responseCode == HttpURLConnection.HTTP_OK) {
           // opens input stream from the HTTP connection
           val inputStream = httpConn.inputStream
           Log.i(TAG, "File content is:\n===========================")
           inputStream.reader().forEachLine {
               Log.i(TAG, it)
           Log.i(TAG, "===========================")
           Log.i(TAG, "File downloaded")
       } else {
           Log.i(TAG, "No file to download. Server replied HTTP code: $responseCode")

    NSString *fileUrl = @"";
    NSURL *url = [NSURL URLWithString:fileUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && response) {
            NSLog(@"File has been downloaded");
        else if (error) {
            NSLog(@"Error: %@", [error localizedDescription]);
    [dataTask resume];

    let fileUrl = ""     
    if let url = URL(string: fileUrl) {
        let request = URLRequest(url: url)
        let urlSession = URLSession(configuration: .default)
        let dataTask = urlSession.dataTask(with: request) { data, response, error in
            if data != nil, response != nil {
                print("File has been downloaded")
            else if let error = error {
                print("Error: \(error.localizedDescription)")

    const Backendless = require('backendless')
     Or use `import Backendless from 'backendless'` for client side.
     If you don't use npm or yarn to install modules, you can add the following line
     <script src="//"></script>
     to your index.html file and use the global Backendless variable.
    Backendless.initApp('YOUR_APP_ID', 'YOUR_JS_API_KEY')
    const fileURL = ''
    const onSuccess = fileContent => {
      console.log('File content is:\n===========================')
    const onError = error => {
      console.error('Server reported an error: ', error.message)
      console.error('error code: ', error.code)
      console.error('http status: ', error.status)

    static void downloadFile(String url) {
       new HttpClient().getUrl(Uri.parse(url))
       .then((HttpClientRequest request) => request.close())
       .then((HttpClientResponse response) =>
         response.transform(utf8.decoder).listen((contents) {
           print("File content is:\n===========================");