NSURLSession 上传任务超时

Posted

技术标签:

【中文标题】NSURLSession 上传任务超时【英文标题】:NSURLSessionUploadTask timing out 【发布时间】:2014-09-24 22:18:05 【问题描述】:

我正在使用 AFNetworking 将视频文件上传到服务器,几乎每次尝试上传文件时都会出现上传超时。

我正在尝试同时上传多个文件,到目前为止我尝试的最大值是 2,因为它们一直超时。

相关代码:

        for i in 0 ... 2 
            let filePath : NSURL = NSURL(fileURLWithPath: "filepathgoeshere")

            let tempFilename = String(format: "%f", NSDate.timeIntervalSinceReferenceDate())
            let tempFileUrl = NSURL(fileURLWithPath: "\(NSTemporaryDirectory())\(tempFilename)")

            let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: getAPIURL(), parameters: parameters, constructingBodyWithBlock:  (formData : AFMultipartFormData!) -> Void in
                formData.appendPartWithFileURL(filePath, name: "file", fileName: "file", mimeType: "video/mp4", error: nil)
                , error: nil)

            // Work around for problem with multi-part requests not giving a content-length and being rejected by S3
            // See: https://github.com/AFNetworking/AFNetworking/issues/1398
            AFHTTPRequestSerializer().requestWithMultipartFormRequest(request, writingStreamContentsToFile: tempFileUrl, completionHandler:  (error: NSError!) -> Void in
                var manager : AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())

                var progress : NSProgress? = nil

                var uploadTask : NSURLSessionUploadTask = manager.uploadTaskWithRequest(request, fromFile: tempFileUrl, progress: &progress, completionHandler:  (response: NSURLResponse!, responseObject: AnyObject!, error: NSError!) -> Void in
                    NSFileManager.defaultManager().removeItemAtURL(tempFileUrl, error: nil)
                    if let err = error 
                        println("There was an error :(")
                        println("Error: \(err.localizedDescription)")
                        // TODO: Add in relevant error catching
                        successCallback(success: false)
                     else 
                        successCallback(success: true)
                    
                )

                if let testNil = progress 
                    progressCallback(progress: progress)
                

                uploadTask.resume()
            )
        

【问题讨论】:

【参考方案1】:
func multipleFileUploadRequestSessionManager(url:String,parameters:NSDictionary,fileInfo:NSArray,completionHandler: (response:NSURLResponse?,responseObject:AnyObject?,error:NSError?) -> ())
    
        let request : NSMutableURLRequest = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: url, parameters: parameters, constructingBodyWithBlock:  (formData) -> Void in

            if(fileInfo.count > 0)
            
                for file in fileInfo
                
                    var fileData    = file.valueForKey("fileData") as NSData
                    var name        = file.valueForKey("filenameData") as String
                    var fileName    = file.valueForKey("fileName") as String
                    var mimeType    = file.valueForKey("mimeType") as String

                    formData.appendPartWithFileData(fileData, name: name, fileName: fileName, mimeType: mimeType)
                
            


        , error: nil)

        let conf : NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

        let manager : AFURLSessionManager = AFURLSessionManager(sessionConfiguration: conf)

        var progress : NSProgress? = nil

        var uploadTask:NSURLSessionUploadTask = manager.uploadTaskWithStreamedRequest(request, progress: nil)  (response, responseObject, error) -> Void in
            completionHandler(response:response,responseObject:responseObject,error:error)

        

        uploadTask.resume()

这可能会帮助您上传多个文件

【讨论】:

以上是关于NSURLSession 上传任务超时的主要内容,如果未能解决你的问题,请参考以下文章

NSURLSession 超时

iOS NSURLSessionConfiguration Reference

无法在 iOS 中使用 NSURLSession 多部分表单数据上传文件

iOS:如何增加 NSURLSession 上传任务的块大小?

使用 NSURLSession UploadTask 暂停、恢复、取消上传任务

NSURLSession : 设置请求超时 > 75 秒 [服务器无法访问]