gpt4 book ai didi

swift - NSURLSessionUploadTask 超时

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:16 25 4
gpt4 key购买 nike

我正在使用 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()
})
}

最佳答案

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()
}

这可能有助于您上传多个文件

关于swift - NSURLSessionUploadTask 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26027479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com