1

I'm trying to download a file with SwiftyDropbox but I have problemas with the path. I have a file in mi Dropbox "prueba.txt":

Dropbox file

And this is the code that I use to download in my app.

import UIKit
import SwiftyDropbox

let clientDB = DropboxClientsManager.authorizedClient

class Controller: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: {
        (url: URL) -> Void in UIApplication.shared.open(url)
    })

    let fileManager = FileManager.default
    let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let destURL = directoryURL.appendingPathComponent("/test.txt")
    let destination: (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
        return destURL
    }

    clientDB?.files.download(path: "/prueba.txt", overwrite: true, destination: destination)
        .response{ response, error in
            if response != nil{
                self.cargarDatosCliente()
                //print (response)
            } else if let error = error{
                print (error)
            }
        }

        .progress{ progressData in
            print(progressData)
        }
    }
}

I try different ways but always obtain the same problem with "path", always the error is path/not_found/... I try with other path but is the same problem. Could you help me? Where is my mistake?

Thanks!

Tim
  • 10,459
  • 4
  • 36
  • 47
sergio
  • 21
  • 3

2 Answers2

1

The problem is that "/prueba.txt" is a local file path. Dropbox expects you to give it a file path for their remote server.

You can retrieve those by using listFolder and listFolderContinue.

For example, if you want to retrieve the file paths in the root folder of your app or dropbox use:

var path = ""
clientDB?.files.listFolder(path: path).response(completionHandler: { response, error in
            if let response = response {
                let fileMetadata = response.entries
                if response.hasMore {
                    // Store results found so far
                    // If there are more entries, you can use `listFolderContinue` to retrieve the rest.
                } else {
                    // You have all information. You can use it to download files.
                }
            } else if let error = error {
                // Handle errors
            }
        })

The fileMetadata contains the path you need. For example, you can get the path to the first file like this:

let path = fileMetadata[0].pathDisplay
gebirgsbärbel
  • 2,327
  • 1
  • 22
  • 38
1

If you're getting metadata about files from the API, this would be the "pathLower" property of a FileMetadata object.

client?.files.download(path: fileMetadata.pathLower!, overwrite: true, destination: destination)
                            .response { response, error in
                                if let response = response {
                                    print(response)
                                } else if let error = error {
                                    print(error)
                                }
                            }