1

I'm using this code to download a file and it works fine.

// Download a file

let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    // generate a unique name for this file in case we've seen it before
    let UUID = NSUUID().UUIDString
    let pathComponent = "\(UUID)-\(response.suggestedFilename!)"
    return directoryURL.URLByAppendingPathComponent(pathComponent)
}

client.files.download(path: "/hello.txt", destination: destination).response { response, error in
    if let (metadata, url) = response {
        print("*** Download file ***")
        let data = NSData(contentsOfURL: url)
        print("Downloaded file name: \(metadata.name)")
        print("Downloaded file url: \(url)")
        print("Downloaded file data: \(data)")
    } else {
        print(error!)
    }
}

My question is how do I get download progress to show to user?

3 Answers 3

5

Adapted from the tutorial, you can add a progress callback on the download method to get progress information:

// Download a file
let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    // generate a unique name for this file in case we've seen it before
    let UUID = NSUUID().UUIDString
    let pathComponent = "\(UUID)-\(response.suggestedFilename!)"
    return directoryURL.URLByAppendingPathComponent(pathComponent)
}

Dropbox.authorizedClient!.files.download(path: "/path/to/Dropbox/file", destination: destination)

    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in

        print("bytesRead: \(bytesRead)")
        print("totalBytesRead: \(totalBytesRead)")
        print("totalBytesExpectedToRead: \(totalBytesExpectedToRead)")

    }

    .response { response, error in

        if let (metadata, url) = response {
            print("*** Download file ***")
            print("Downloaded file name: \(metadata.name)")
            print("Downloaded file url: \(url)")
        } else {
            print(error!)
        }

    }

You can then use that raw progress information to back the progress UI in your app.

3

On Swift3, Dropbox API V2 has updated, .progress is updated to progressData as NSProgress.

Dropbox Official Information https://github.com/dropbox/SwiftyDropbox#upload-style-request

NSProgress Apple API Reference https://developer.apple.com/reference/foundation/progress

So, Greg's Sample is going to update like this,

dropboxClient.files.files.download(path: "/path/to/Dropbox/file", destination: destination)

.progress { progressData in

    print("bytesRead = totalUnitCount: \(progressData.totalUnitCount)")
    print("totalBytesRead = completedUnitCount: \(progressData.completedUnitCount)")

    print("totalBytesExpectedToRead (Has to sub): \(progressData.totalUnitCount - progressData.completedUnitCount)")

    print("progressData.fractionCompleted (New)  = \(progressData.fractionCompleted)")

}

.response { response, error in

    if let (metadata, url) = response {
        print("*** Download file ***")
        print("Downloaded file name: \(metadata.name)")
        print("Downloaded file url: \(url)")
    } else {
        print(error!)
    }

}

0

In swift 3, We should download like below format

let path = "/Files/\(textTitle).txt"


          _ =  client?.files.download(path: path).response{response,error in

                if let response = response{

                    let responseMetadata = response.0

                    let filecontents = response.1 . // File in encrypt format

                    let description = String(data: filecontents, encoding: String.Encoding.utf8)!   //Decrypting the file


                }else if let error = error{

                    print("The Download error is \(error)")
                }

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.