I am using NSURLSessionUploadTask to upload a file.
Here are some parts of my code not complete
let session:NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue .mainQueue())
let sessionTask:NSURLSessionUploadTask = session.uploadTaskWithStreamedRequest(request
But the problem is I am unable to get the JSON response the server sends back.
The following delegate also not firing but other delegates are firing
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData)
Code that I am using:
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")
}
I have conformed to the delegates
- NSURLSessionDataDelegate
- NSURLSessionDelegate
- NSURLSessionTaskDelegate
Thanks
NSURLSessionDataTask
as you are Posting data to server using API, you are not uploading/transfering any resource to storage. Lets say if you upload a video to amazon bucket at that time you can useNSURLSessionUploadTask
which is meant for Uploading. Here you are Posting Image, video etc in API request. – Twine