我正在尝试同时上传多个文件,到目前为止,我尝试的最大值是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() }) }解决方法
func multiplefileUploadRequestSessionManager(url:String,parameters:NSDictionary,fileInfo:NSArray,completionHandler: (response:NSURLResponse?,responSEObject:AnyObject?,error:NSError?) -> ()) { let request : NSMutableURLRequest = AFhttpRequestSerializer().multipartFormRequestWithMethod("POST",URLString: url,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()}
这可能有助于您进行多个文件上传
总结以上是内存溢出为你收集整理的swift – NSURLSessionUploadTask超时全部内容,希望文章能够帮你解决swift – NSURLSessionUploadTask超时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)