We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

rabbigarfinkel's avatar
rabbigarfinkel
Explorer | Level 4
9 years ago
Solved

Downloading file with Swiftydropbox

I have successfully installed Swiftydropbox and my app successfully registers with Dropbox.  But I don't understand what to do now if I want to download from a Dropbox. My goal is to download a text file into a URL and then open that URL in a tableview.

 

I think where I'm having trouble understanding the docs is this line:

 

 client.files.download(path: "/test/path/in/Dropbox/account", overwrite: true, destination: destination)

What goes in place of /test/path/in/Dropbox/account?  For instance, how would I get the app to download something from my own account?

 

Thank you,

 

Eli

  • Greg-DB's avatar
    Greg-DB
    9 years ago

    That code doesn't list or display any files. It only downloads a file at "/Documents/RamKolText2.txt".

     

    If you want to get a list of files to display to the user, you can use listFolder and listFolderContinue.

5 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

    That path should be the remote path of the file in the connected Dropbox that you want to download. For example, to get a file named "example.txt" in a folder named "Documents", you would supply "/Documents/example.txt".

     

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

  • rabbigarfinkel's avatar
    rabbigarfinkel
    Explorer | Level 4
    9 years ago

    Thank you.  The problem is that I would not know the filepath of a user's selected file.  In the code below, what line is supposed to trigger the display of the user's files so that the user can tap on a file and return that path to my app?

     

    // Download to URL
            let fileManager = FileManager.default
            let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let destURL = directoryURL.appendingPathComponent("/Documents/RamKolText2.txt")
            let destination: (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
                return destURL
            }
            client.files.download(path: "/Documents/RamKolText2.txt", overwrite: true, destination: destination)
                .response { response, error in
                    if let response = response {
                        print(response)
                    } else if let error = error {
                        print(error)
                    }
                }
                .progress { progressData in
                    print(progressData)
            }

     

    Thanks! 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

    That code doesn't list or display any files. It only downloads a file at "/Documents/RamKolText2.txt".

     

    If you want to get a list of files to display to the user, you can use listFolder and listFolderContinue.

  • AbrahamEP's avatar
    AbrahamEP
    New member | Level 2
    9 years ago

    I had the same problem. It took me so long to find the answer for that, really it was overwhelming because there isn't much info about it. I did this: 

    I'm sorry for my english, is not my first language.

     

    Install Dropbox Chooser https://www.dropbox.com/developers/chooser#ios 

    Install SwiftyDropbox

     

    SwiftyDropbox installs Alamofire, we need this to download the file, if not then install it 

     

    Dropbox Chooser allow the user select any file from their dropbox account and returns the shared link of the selected file so: 

     

    //Ask SwiftyDropbox if the user is logged in

                if DropboxClientsManager.authorizedClient != nil{

                    

     

    // Use this from Dropbox chooser to open a view controller that allow the user select the file

    //DBChooserLinkTypeDirect <--- it has to be this parameter, there is another one but is useless for our purpose

                    DBChooser.default().open(for: DBChooserLinkTypeDirect, from: self, completion: { (results) in

                        

                        //Here starts the completion block of Chooser

     

                        if let resultado = results{ //Obtain the results 

                            

                            var d = DBChooserResult() //Create a variable of DBChooserResult type

                            d = resultado[0] as! DBChooserResult //Get the result. Just one result because the user just can select ONE file (in this version just one, it may be more but I don't know how in this moment

                            print(d.name)

                            print(d.link)//The atributte link give us the url to download the file. If you paste it in your browser the download starts right away

                            

     

                          Use Alamofire to download the file from the url

                            Alamofire.request(d.link).responseData { response in

                               

                 //Get the data from the URL

                                if let data = response.result.value

                                {

                                   //Here we have the data to do whatever we want, in my case I show the PDF file in a WebView

                                    print("Data de Alamofire \(response.description)")

                                    self.desplegarUrlEnWebview(datos: data, url: d.link)

                                }

                            }

                        }

     

                    })

                }else{

                    

                    //If the user is not logged in we use SwiftyDropbox to log in

                    

                    DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: { (url) in

                        

                        UIApplication.shared.open(url, options: [:], completionHandler: nil)

                    })

                }

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,035 PostsLatest Activity: 12 hours ago
410 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!