ios - NSURLSessionUploadTask how to read server response -


i using nsurlsessionuploadtask upload file.

here parts of code not complete

let session:nsurlsession = nsurlsession(configuration: config, delegate: self, delegatequeue: nsoperationqueue .mainqueue())  let sessiontask:nsurlsessionuploadtask = session.uploadtaskwithstreamedrequest(request 

but problem unable json response server sends back.

the following delegate not firing other delegates firing

func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceivedata data: nsdata) 

code 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 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 delegates

  1. nsurlsessiondatadelegate
  2. nsurlsessiondelegate
  3. nsurlsessiontaskdelegate

thanks

you shouldn't using uploadtaskwithstreamedrequest: if you're creating data when create request. that's intended uploading huge chunks of data need read data file , encode bit @ time, sending out bit @ time. (and mentioned, have provide neednewbodystream method if that.)

chances are, should using uploadtaskwithrequest:fromdata: , providing body data blob fromdata parameter.

you don't need set body data in request. nsurlsession ignores rule.

you might consider uploadtaskwithrequest:fromdata:completionhandler:, let specify block run entire data when upload finished, saving having provide delegate method accumulate data.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -