我正在创建一个互联网速度测试应用程序,似乎无法正确地将zip文件上传到ftp服务器。它是一个开放的服务器。我希望能够上传文件并显示进度。NSURLSession上传文件到服务器Swift
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
不起作用。
import UIKit
import Foundation
class Upload: UIViewController, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate {
let url: NSURL = NSURL(string: "ftp://speedtest.tele2.net/upload")!
let path = NSBundle.mainBundle().pathForResource("2MB", ofType: "zip")
func Test() {
let data: NSData = NSData(contentsOfFile: path!)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
request.timeoutInterval = 10.0
request.HTTPBody = data
let session: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let task: NSURLSessionUploadTask = session.uploadTaskWithRequest(request, fromData: data)
task.resume()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
print(error)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(NSURLSessionResponseDisposition.Allow)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
//
}
}
好吧没有更多的错误,但'didSendBodyData'不工作? – HovyTech
您确定网站接受您的数据就像那样。 – Sandeep
这是一个ftp服务器,我不确定,如何检查它接受的数据? – HovyTech