簡體   English   中英

使用Swift將文件從iOS應用上傳到Dropbox

[英]upload files to Dropbox from iOS app with Swift

我已經完成了本教程( https://blogs.dropbox.com/developers/2014/09/swift-apps-with-dropbox/ ),並成功將我的iOS應用程序與Dropbox關聯了。 但是,我想將文件從我的應用程序上傳到Dropbox。 那里的所有教程都只有Objective C中的代碼,包括Dropbox中的主要教程( https://www.dropbox.com/developers/core/start/ios )。 有誰知道如何用Swift做到這一點?

謝謝!

有用。

let textContent = "Hello Swift Upload"

let textData:NSData? = textContent.dataUsingEncoding(NSUTF8StringEncoding)

var client:DropboxClient? = Dropbox.authorizedClient

if let cli = client {
    cli.files.upload(path: "/Swift-Upload.txt", mode: Files.WriteMode.Add, autorename: false, clientModified: nil, mute: false, body: textData!)
}

iOS 10.12.3 swift 3.0 SwiftyDropbox 4.1.1一年以后,答案會稍微完整一些。

 func files_saver(sourcePath: String) {
    let textContent = "Blah Blah Blah"
    let textData:NSData? = textContent.data(using: String.Encoding.utf8) as NSData?

    let client = DropboxClientsManager.authorizedClient!
    client.files.upload(path: sourcePath, input: textData as! Data).response { response, error in
        if let metadata = response {
            print("Uploaded file name: \(metadata.name)")
            print("Uploaded file revision: \(metadata.rev)")

            // Get file (or folder) metadata
        }
        if let error = error {
            switch error as! CallError<SwiftyDropbox.Files.UploadError> {
            case .routeError(let boxed, let requestId):
                switch boxed.unboxed {
                case .path(let failedPath):
                        //print("Failed update 2 path: \(failedPath)")
                        NotificationCenter.default.post(name: Notification.Name("dbFileCreationError"), object: nil, userInfo: nil)
                        break

                    default:
                        //print("Unknown \(error)")
                        break
                    }
            case .internalServerError(let code, let message, let requestId):
                //print("InternalServerError[\(requestId)]: \(code): \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbInternalServerError"), object: nil, userInfo: nil)
                break
            case .badInputError(let message, let requestId):
                //print("BadInputError[\(requestId)]: \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbBadInputError"), object: nil, userInfo: nil)
                break
            case .authError(let authError, let requestId):
                //print("AuthError[\(requestId)]: \(authError)")
                NotificationCenter.default.post(name: Notification.Name("dbAuthError"), object: nil, userInfo: nil)
                break
            case .rateLimitError(let rateLimitError, let requestId):
                //print("RateLimitError[\(requestId)]: \(rateLimitError)")
                NotificationCenter.default.post(name: Notification.Name("dbRateLimitError"), object: nil, userInfo: nil)
                break
            case .httpError(let code, let message, let requestId):
                //print("HTTPError[\(requestId)]: \(code): \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbHTTPError"), object: nil, userInfo: nil)
                break
            default:
                break
                }
        }
    }
}

我能夠使用此(要點)代碼(Swift 2.2)上傳大文件(> 800MB)->

https://gist.github.com/cnharris10/3d744ca13abd13d4d5bd3a363be16dff

請參閱下面的示例屏幕快照,其中另一個150mb文件以1mb的塊上傳

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM