Afnetworking to implement a breakpoint when the program restarts

Source: Internet
Author: User

  1. Get the downloaded file size
  2. -(unsigned long long) Filesizeforpath: (NSString *) path {
  3. signed long long fileSize = 0;
  4. Nsfilemanager *filemanager = [Nsfilemanager new]; //default is not thread safe
  5. if ([FileManager Fileexistsatpath:path]) {
  6. nserror *error = nil;
  7. nsdictionary *filedict = [FileManager attributesofitematpath:path error:&error];
  8. if (!error && filedict) {
  9. FileSize = [filedict fileSize];
  10. }
  11. }
  12. return fileSize;
  13. }
  14. Start download
  15. -(void) Startdownload {
  16. nsstring *downloadurl = @ "Http://www.xxx.com/xxx.zip";
  17. nsstring *cachedirectory = [Nssearchpathfordirectoriesindomains (nscachesdirectory, NSUserDomainMask, YES)  Objectatindex:0];
  18. nsstring *downloadpath = [cachedirectory stringbyappendingpathcomponent:@ "Xxx.zip"];
  19. nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl Urlwithstring:downloadurl];
  20. //Check whether the file has been downloaded partially
  21. Unsigned long long downloadedbytes = 0;
  22. if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:downloadpath]) {
  23. //Get the downloaded file length
  24. Downloadedbytes = [self filesizeforpath:downloadpath];
  25. if (Downloadedbytes > 0) {
  26. nsmutableurlrequest *mutableurlrequest = [request mutablecopy];
  27. nsstring *requestrange = [NSString stringwithformat:@ "bytes=%llu-", downloadedbytes];
  28. [Mutableurlrequest setvalue:requestrange Forhttpheaderfield:@ "Range"];
  29. request = Mutableurlrequest;
  30. }
  31. }
  32. //Do not use the cache, avoid the problem of the breakpoint continued to pass
  33. [[Nsurlcache Sharedurlcache] removecachedresponseforrequest:request];
  34. //Download Request
  35. afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request];
  36. //download Path
  37. Operation. outputstream = [Nsoutputstream outputstreamtofileatpath:downloadpath append:YES];
  38. //Download Progress callback
  39. [Operation setdownloadprogressblock:^ (Nsuinteger bytesread, long Long totalbytesread, long long Totalbytesexpectedtoread) {
  40. //Download Progress
  41. Float progress = ((float) Totalbytesread + downloadedbytes)/(Totalbytesexpectedtoread + downloadedbytes);
  42. }];
  43. //Success and failure callbacks
  44. [Operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID responseobject) {
  45. } failure:^ (afhttprequestoperation *operation, nserror *error) {
  46. }];
  47. [Operation start];
  48. }

It is important to note that this is only useful for downloading zip packages, because there may be too many data to download files in other formats. When the file has been downloaded, call the function again, unable to determine whether the file has been downloaded completely, so it will be downloaded again, the server will report 416 error, the return will also output to the file, so that the file size is abnormal. However, the ZIP format is not affected.

Afnetworking to implement a breakpoint when the program restarts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.