当前位置: 首页 > 编程笔记 >

Dropbox API 使用SwiftyDropbox库从NSData上传文件并处理所有错误情况

晋鹤轩
2023-03-14
本文向大家介绍Dropbox API 使用SwiftyDropbox库从NSData上传文件并处理所有错误情况,包括了Dropbox API 使用SwiftyDropbox库从NSData上传文件并处理所有错误情况的使用技巧和注意事项,需要的朋友参考一下

示例

这使用SwiftyDropbox库将文件从上载到NSDataDropbox帐户,并使用较大文件的上载会话处理每个错误情况:

import UIKit
import SwiftyDropbox

class ViewController: UIViewController {

    // 用真实数据替换组成的数据
    let data = String(count: 20 * 1024 * 1024, repeatedValue: Character("A")).dataUsingEncoding(NSUTF8StringEncoding)!


    let chunkSize = 5 * 1024 * 1024  // 5兆字节
    var offset = 0
    var sessionId = ""

    // 将其替换为所需的目标路径:
    let destPath = "/SwiftyDropbox_upload.txt"

    override func viewDidLoad() {
        super.viewDidLoad()

       Dropbox.authorizedClient= DropboxClient(...)

        doUpload()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func doUpload() {

        let fileSize = data.length

        print("Have \(fileSize) bytes to upload.")

        if (fileSize < chunkSize) {

            print("Using non-chunked uploading...")

            Dropbox.authorizedClient!.files.upload(path:destPath, input:data).response { response, error in
                if let metadata = response {
                    print(metadata)
                } else if let callError = error {
                    print("upload failed")
                    switch callError as CallError {
                    case .RouteError(let boxed, let requestId):
                        print("RouteError[\(requestId)]:")
                        switchboxed.unboxedasFiles.UploadError{
                        case .Path(let uploadError):
                            print("Path:")
                            switchuploadError.reasonasFiles.WriteError{
                            case .MalformedPath(let malformedPathError):
                                print("MalformedPath: \(malformedPathError)")
                            case .Conflict(let writeConflictError):
                                print("Conflict:")
                                switch writeConflictError {
                                case .File:
                                    print("File")
                                case .FileAncestor:
                                    print("FileAncestor")
                                case .Folder:
                                    print("Folder")
                                case .Other:
                                    print("Other")
                                }
                            case .DisallowedName:
                                print("DisallowedName")
                            case .InsufficientSpace:
                                print("InsufficientSpace")
                            case .NoWritePermission:
                                print("NoWritePermission")
                            case .Other:
                                print("Other")
                            }
                        case .Other:
                            print("Other")
                        }
                    case .BadInputError(let message, let requestId):
                        print("BadInputError[\(requestId)]: \(message)")
                    case .HTTPError(let code, let message, let requestId):
                        print("HTTPError[\(requestId)]: \(code): \(message)")
                    case .InternalServerError(let code, let message, let requestId):
                        print("InternalServerError[\(requestId)]: \(code): \(message)")
                    case .OSError(let err):
                        print("OSError: \(err)")
                    case .RateLimitError:
                        print("RateLimitError")
                    }
                }
            }

        } else {

            print("使用分块上传...")

            uploadFirstChunk()

        }
    }

    func uploadFirstChunk() {
        let size = min(chunkSize, data.length)
        Dropbox.authorizedClient!.files.uploadSessionStart(input:
            data.subdataWithRange(NSMakeRange(0, size)))
            .response { response, error in
                if let result = response {
                   self.sessionId= result.sessionId
                   self.offset+= size
                    print("So far \(self.offset) bytes have been uploaded.")
                    self.uploadNextChunk()
                } else if let callError = error {
                    print("uploadSessionStart failed")
                    switch callError as CallError {
                    case .RouteError(let error, let requestId):
                        print("RouteError[\(requestId)]: \(error)")
                    case .BadInputError(let message, let requestId):
                        print("BadInputError[\(requestId)]: \(message)")
                    case .HTTPError(let code, let message, let requestId):
                        print("HTTPError[\(requestId)]: \(code): \(message)")
                    case .InternalServerError(let code, let message, let requestId):
                        print("InternalServerError[\(requestId)]: \(code): \(message)")
                    case .OSError(let err):
                        print("OSError: \(err)")
                    case .RateLimitError:
                        print("RateLimitError")
                    }
                }
        }
    }

    func uploadNextChunk() {
        ifdata.length- offset <= chunkSize {
            let size =data.length- offset
            Dropbox.authorizedClient!.files.uploadSessionFinish(
                cursor: Files.UploadSessionCursor(
                    sessionId: self.sessionId, offset: UInt64(offset)),
                commit: Files.CommitInfo(path:destPath),
                input: data.subdataWithRange(NSMakeRange(offset, size)))
                .response { response, error in
                    if let callError = error {
                        print("uploadSessionFinish failed")
                        switch callError as CallError {
                        case .RouteError(let boxed, let requestId):
                            print("RouteError[\(requestId)]:")
                            switchboxed.unboxedasFiles.UploadSessionFinishError{
                            case .Path(let writeError):
                                print("Path: ")
                                switch writeError {
                                case .MalformedPath(let malformedPathError):
                                    print("MalformedPath: \(malformedPathError)")
                                case .Conflict(let writeConflictError):
                                    print("Conflict:")
                                    switch writeConflictError {
                                    case .File:
                                        print("File")
                                    case .FileAncestor:
                                        print("FileAncestor")
                                    case .Folder:
                                        print("Folder")
                                    case .Other:
                                        print("Other")
                                    }
                                case .DisallowedName:
                                    print("DisallowedName")
                                case .InsufficientSpace:
                                    print("InsufficientSpace")
                                case .NoWritePermission:
                                    print("NoWritePermission")
                                case .Other:
                                    print("Other")
                                }
                            case .LookupFailed(let uploadSessionLookupError):
                                print("LookupFailed:")
                                switch uploadSessionLookupError {
                                case .Closed:
                                    print("Closed")
                                case .IncorrectOffset(let uploadSessionOffsetError):
                                    print("IncorrectOffset: \(uploadSessionOffsetError)")
                                case .NotFound:
                                    print("NotFound")
                                case .NotClosed:
                                    print("NotFound")
                                case .Other:
                                    print("Other")
                                }
                            case .TooManySharedFolderTargets:
                                print("TooManySharedFolderTargets")
                            case .Other:
                                print("Other")
                            }
                        case .BadInputError(let message, let requestId):
                            print("BadInputError[\(requestId)]: \(message)")
                        case .HTTPError(let code, let message, let requestId):
                            print("HTTPError[\(requestId)]: \(code): \(message)")
                        case .InternalServerError(let code, let message, let requestId):
                            print("InternalServerError[\(requestId)]: \(code): \(message)")
                        case .OSError(let err):
                            print("OSError: \(err)")
                        case .RateLimitError:
                            print("RateLimitError")
                        }
                    } else if let result = response {
                        print("Done!")
                        print(result)
                    }
            }
        } else {
            Dropbox.authorizedClient!.files.uploadSessionAppendV2(
                cursor: Files.UploadSessionCursor(sessionId: self.sessionId, offset: UInt64(offset)),
                input: data.subdataWithRange(NSMakeRange(offset, chunkSize)))
                .response { response, error in
                    if error == nil {
                       self.offset+= self.chunkSize
                        print("So far \(self.offset) bytes have been uploaded.")
                        self.uploadNextChunk()
                    } else if let callError = error {
                        print("uploadSessionAppend failed")
                        switch callError as CallError {
                        case .RouteError(let boxed, let requestId):
                            print("RouteError[\(requestId)]:")
                            switchboxed.unboxedasFiles.UploadSessionLookupError{
                            case .Closed:
                                print("Closed")
                            case .IncorrectOffset(let uploadSessionOffsetError):
                                print("IncorrectOffset: \(uploadSessionOffsetError)")
                            case .NotFound:
                                print("NotFound")
                            case .NotClosed:
                                print("NotClosed")
                            case .Other:
                                print("Other")
                            }
                        case .BadInputError(let message, let requestId):
                            print("BadInputError[\(requestId)]: \(message)")
                        case .HTTPError(let code, let message, let requestId):
                            print("HTTPError[\(requestId)]: \(code): \(message)")
                        case .InternalServerError(let code, let message, let requestId):
                            print("InternalServerError[\(requestId)]: \(code): \(message)")
                        case .OSError(let err):
                            print("OSError: \(err)")
                        case .RateLimitError:
                            print("RateLimitError")
                        }
                    }
            }
        }
    }

}
           

 类似资料:
  • 本文向大家介绍Dropbox API 使用SwiftyDropbox库下载处理了所有错误情况的文件,包括了Dropbox API 使用SwiftyDropbox库下载处理了所有错误情况的文件的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 本文向大家介绍PHP 处理文件上传错误,包括了PHP 处理文件上传错误的使用技巧和注意事项,需要的朋友参考一下 示例 该$_FILES["FILE_NAME"]['error'](其中"FILE_NAME"是文件输入,存在于你的表单的名称属性的值)可能包含以下值之一: UPLOAD_ERR_OK -没有错误,文件上传成功。 UPLOAD_ERR_INI_SIZE-上载的档案超过中的upload_m

  • 问题内容: 我正在使用multer将文件保存在通过express&nodejs开发的服务器上。 我使用下面的代码。 Multer会为我将文件保存在指定的目标文件夹中。 所有这些都工作正常,但我有以下问题: 如果文件保存由于各种原因而失败,则看来我的路线将始终返回状态204。 我不确定在保存文件后或异步获取文件时是否取消状态204,是否返回状态204。 问题答案: 您可以使用以下选项处理错误: 如果

  • 本文向大家介绍Dropbox API 与使用SwiftyDropbox库处理的每个错误案例共享一个文件夹,包括了Dropbox API 与使用SwiftyDropbox库处理的每个错误案例共享一个文件夹的使用技巧和注意事项,需要的朋友参考一下 示例 这使用SwiftyDropbox库共享一个文件夹,处理所有错误情况:            

  • 问题内容: 我最近刚开始玩go,所以我还是菜鸟,对不起,如果我犯了太多错误。我已经尝试解决了很长时间,但我只是不明白发生了什么。在我的main.go文件中,我有一个主要功能: 处理程序函数如下所示: 我知道这不是服务网站的最佳方法提交功能如下所示: 问题是,当执行Submit 函数时,它是并且是一个空字符串,如果r.FormFile返回以下错误,它将继续执行直到第一个: 我不明白为什么r.Meth

  • 问题内容: 用Flask处理超大文件上传(1 GB以上)的最佳方法是什么? 我的应用程序实际上需要多个文件,为它们分配一个唯一的文件号,然后根据用户选择的位置将其保存在服务器上。 我们如何将文件上传作为后台任务运行,以使用户在1小时内没有浏览器旋转,而是可以立即进入下一页? Flask开发服务器能够处理大量文件(50gb需要1.5个小时,上传速度很快,但将文件写入空白文件的速度却很慢) 如果我用T