NSURLSession将文件上传到服务器Swift [英] NSURLSession Upload File To Server Swift

查看:114
本文介绍了NSURLSession将文件上传到服务器Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个互联网速度测试应用程序,似乎无法将zip文件上传到ftp服务器。它是一个开放的服务器。我希望能够上传文件并显示进度。


$ b

func URLSession(会话:NSURLSession,任务:NSURLSessionTask,didSendBodyData bytesSent: Int64,totalBytesSent:Int64,totalBytesExpectedToSend:Int64)不起作用。

  import UIKit 
进口基金会

类上传:UIViewController中,NSURLSessionTaskDelegate,NSURLSessionDataDelegate,NSURLSessionDelegate {

让利网址:NSURL = NSURL(字符串:ftp://speedtest.tele2.net /上传)!
let path = NSBundle.mainBundle()。pathForResource(2MB,ofType:zip)

func Test(){
let data:NSData = NSData(contentsOfFile :路径!)!

让请求:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = POST
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
request.timeoutInterval = 10.0
request.HTTPBody = data

让session:NSURLSession = NSURLSession(配置: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类型){
打印( \(totalBytesSent)/ \(totalBytesExpectedToSend))
}

FUNC URLSession(会话:NSURLSession,任务:NSURLSessionTask,didCompleteWithError错误:NSErro ΔR){
打印(误差)$​​ B $ B}

FUNC URLSession(会话:NSURLSession,dataTask:NSURLSessionDataTask,didReceiveResponse响应:NSURLResponse,completionHandler:(NSURLSessionResponseDisposition) - > (会话:NSURLSession,dataTask:NSURLSessionDataTask,didReceiveData数据:NSData){
//
}

}


解决方案

您是否正确阅读了文档。如果你看看这个方法的文档,

  func URLSession(session:NSURLSession,dataTask:NSURLSessionDataTask,didReceiveResponse response:NSURLResponse, completionHandler:(NSURLSessionResponseDisposition) - > Void){




除非您需要支持(相对
模糊)multipart / x-mixed-replace内容类型,否则可选。使用该内容
类型,服务器发送一系列部分,其中每个部分用于
替换前一部分。会话在每个零件的
开头调用这个方法,然后你应该显示,放弃或者
,否则按照情况处理前面的零件。



如果您没有提供此委托方法,则会话始终允许
任务继续。


这里, https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveResponse :completionHandler

您可以简单地不实施此方法,或者必须这样才能允许进一步的响应。

  func URLSession(会话:NSUR LSession,dataTask:NSURLSessionDataTask,didReceiveResponse响应:NSURLResponse,completionHandler:(NSURLSessionResponseDisposition) - > Void){
completionHandler(.Allow)
}


I'm creating a Internet Speed testing app and can't seem to get it right to upload the zip file to the ftp server. It is an open server. I want to be able to upload the file and show the progress.

The func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) isn't working.

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) {
        //
    }

}

解决方案

Have you read the documentation properly. If you look at the documentation for this method,

   func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {

This method is optional unless you need to support the (relatively obscure) multipart/x-mixed-replace content type. With that content type, the server sends a series of parts, each of which is intended to replace the previous part. The session calls this method at the beginning of each part, and you should then display, discard, or otherwise process the previous part, as appropriate.

If you do not provide this delegate method, the session always allows the task to continue.

Here, https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveResponse:completionHandler:

You can simply not implement this method or then have to so that it allows further response.

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
    completionHandler(.Allow)
}

这篇关于NSURLSession将文件上传到服务器Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆