我正在使用 NSURLSessionUploadTask 上传文件。
以下是我的代码的某些部分未完整
let session:NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue .mainQueue())
let sessionTask:NSURLSessionUploadTask = session.uploadTaskWithStreamedRequest(request
但问题是我无法获得服务器发回的JSON响应。
以下代表也没有解雇,但其他代表正在解雇
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData)
我正在使用的代码:
func sendFileToServer1(fileName:String,fileData:NSData,serverURL:String){
let body = NSMutableData()
let mimetype = "application/octet-stream"
// let mimetype = "video/quicktime"
let boundary = "Boundary-\(NSUUID().UUIDString)"
let url = NSURL(string: serverURL)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("multipart/form-data; boundary=----\(boundary)", forHTTPHeaderField: "Content-Type")
body.appendData("------\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"file\"; filename=\"\(fileName)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(fileData)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("------\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"submit\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Submit\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("------\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody=body
let config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session:NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue .mainQueue())
let sessionTask:NSURLSessionUploadTask = session.uploadTaskWithStreamedRequest(request)
sessionTask.resume()
}
func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
print("error")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
print("Bytes sent:\(bytesSent) Total bytes sent:\(totalBytesSent) Total bytes expected to send:\(totalBytesExpectedToSend)")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("response:\(response as! NSHTTPURLResponse)")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("data didReceiveData")
}
我符合代表
由于
答案 0 :(得分:0)
由于NSURLSessionUploadTask
是NSURLSessionDataTask
的子类,您可以尝试使用NSURLSessionDataDelegate
中的方法:
func urlSession(_ session: NSURLSession, dataTask: NSURLSessionDataTask, didReceive response: NSURLResponse, completionHandler: (NSURLSession.ResponseDisposition) -> Void)
根据文件:
告诉代理人数据任务从服务器收到初始回复(标题)。