ios - Can't get the total length of downloading file -
nsmutableurlrequest *objrequest =[[nsmutableurlrequest alloc]initwithurl:url1]; [objrequest sethttpmethod:@"post"]; [objrequest sethttpbody:[nsdata datawithbytes:(__bridge const void *)(dict) length:[dict count]]]; nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:[nsurlrequest requestwithurl:url1] delegate:self]; [connection start]; -(void) connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response{ length = [response expectedcontentlength]; nslog(@"%f",length); } i length -1 in didreceiveresponse method data of downloaded file perfect ...pls tell me how can total length of file downloded..
the expected content length provided server, ios app doesn't have control of whether it's provided or not. must gracefully handle value of nsurlresponseunknownlength, i.e. -1, if length can’t determined.
as the documentation says:
some protocol implementations report content length part of response, not protocols guarantee deliver amount of data. clients should prepared deal more or less data.
as desdenova points out, answer this question points out expectedcontentlength can nsurlresponseunknownlength if content-encoding of response gzip. can disable gzip, clearing request's accept-encoding:
[request setvalue:@"" forhttpheaderfield:@"accept-encoding"]; note, if this, response larger (e.g. in example of small jpg i'm downloading server, gzipped payload 15k, un-gzipped payload 25k). difference may not observable small responses (but then, again, if small, don't need progress updates), large responses, may become material.
Comments
Post a Comment