首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftyDropbox下载进度

SwiftyDropbox下载进度
EN

Stack Overflow用户
提问于 2016-02-26 18:49:55
回答 3查看 2K关注 0票数 1

我正在使用这段代码下载一个文件,它可以正常工作。

代码语言:javascript
复制
// 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!)
    }
}

我的问题是如何使下载进度显示给用户?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-26 19:22:18

tutorial改编而来,您可以在download方法上添加一个progress回调以获取进度信息:

代码语言:javascript
复制
// 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!)
        }

    }

然后,您可以使用原始进度信息来支持应用程序中的进度UI。

票数 5
EN

Stack Overflow用户

发布于 2016-11-21 07:43:33

在Swift3上,Dropbox V2已经更新,.progress作为NSProgress被更新为progressData。

Dropbox官方信息https://github.com/dropbox/SwiftyDropbox#upload-style-request

NSProgress Apple参考https://developer.apple.com/reference/foundation/progress

格雷格的样本会像这样更新,

代码语言:javascript
复制
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!)
    }

}

票数 3
EN

Stack Overflow用户

发布于 2017-05-23 07:53:12

在第三版中,我们应该下载如下格式

代码语言:javascript
复制
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)")
                }

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35659454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档