超时/丢失连接后快速恢复 Alamofire 上传

2023-12-31

我使用 swift 2.3 和 Alamofire 与 multipartFormData 异步上传文件,似乎无法弄清楚在我的连接超时或失去连接并在手机上重新建立连接后是否可以恢复。

我想要的是如果失败则恢复上传,例如:我上传一个大文件。上传到 55% 时,我就失去了互联网连接。当我重新连接互联网时,我希望上传从 55% 继续,而不是再次从 0% 开始。

以下是我用于上传文件的代码:

class FileUploadHelper
{
    class func Upload(numberOfTimes: Int, index: Int, command: String, nsData: NSData, fileName: String, mimeType: String, progress: (index: Int, progressPercentage: Int) -> Void, error: (index: Int, fileUploadError : FileUploadError) -> Void, completed: (index: Int, dataFail: Int) -> Void, badConnection: () -> Void)
    {
        Alamofire.upload(.POST, ResourceHelper.ServiceBaseUrl + command, multipartFormData: 
            { 
                multipartFormData in
                multipartFormData.appendBodyPart(data: nsData, name: "test", fileName: fileName, mimeType: mimeType)
            }, encodingCompletion:
            {
                encodingResult in 
                encodingCompleted(numberOfTimes, index: index, command: command, nsData: nsData, fileName: fileName, mimeType: mimeType, encodingResult: encodingResult, progress: progress, error: error, completed: completed, badConnection: badConnection)
            }
        )
    }

    class func encodingCompleted( numberOfTimes: Int, index: Int, command: String, nsData: NSData, fileName: String, mimeType: String , encodingResult: Manager.MultipartFormDataEncodingResult, progress: (index: Int, progressPercentage: Int) -> Void, error: (index: Int, fileUploadError : FileUploadError) -> Void, completed: (index: Int, dataFail: Int) -> Void, badConnection: () -> Void)
    {
        switch encodingResult {
            case .Success (let upload, _, _):
                upload.responseString {
                    response in
                    var out: NSInteger = 0
                    response.data!.getBytes(&out, length: sizeof(NSInteger))

                    switch response.result {
                        case .Success:
                            dispatch_async(dispatch_get_main_queue(), {
                                completed(index: index, dataFail: out)
                            })
                        case .Failure:
                            if (numberOfTimes > 0) {
                                AWBanner.showWithDuration(7, delay: 0, message: NSLocalizedString("Bad Connection - Attempting to upload", comment: ""), backgroundColor: UIColor.redColor(), textColor: UIColor.whiteColor(), originY: 40.0)
                                FileUploadHelper.Upload(numberOfTimes - 1, index: index, command: command, nsData: nsData, fileName: fileName,mimeType: mimeType, progress: progress, error: error, completed: completed, badConnection: badConnection)
                            }
                            else {
                                badConnection()
                            }
                    }
                }

                upload.progress
                {
                    bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                    dispatch_async(dispatch_get_main_queue(), { 
                    progress(index: index, progressPercentage: Int(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) * 100))

                    })
                }
            case .Failure(let encodingError):
                dispatch_async(dispatch_get_main_queue(), {
                    error(index: index, fileUploadError: FileUploadError.Failed(encodingError: encodingError))      
                })
        }
    }
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

超时/丢失连接后快速恢复 Alamofire 上传 的相关文章

随机推荐