File Download (large file download)

Source: Internet
Author: User

#import "SZMViewController.h"@interfaceSzmviewcontroller () <NSURLSessionDownloadDelegate>@end@implementationSzmviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.}//Task: Any request is a task//nsurlsessiondatatask: normal get\post request//nsurlsessiondownloadtask: File Download//nsurlsessionuploadtask: File Upload- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{[self downloadTask2];}- (void) downloadtask2{nsurlsessionconfiguration*cfg =[Nsurlsessionconfiguration defaultsessionconfiguration]; //1. Get the Session objectNsurlsession *session = [Nsurlsession sessionwithconfiguration:cfgDelegate: Self delegatequeue:[nsoperationqueue mainqueue]]; //2. Create a download taskNsurl *url = [Nsurl urlwithstring:@"Http://***********.mp4"]; Nsurlsessiondownloadtask*task =[Session Downloadtaskwithurl:url]; //3. Start a task[Task resume]; //If the download task is set Completionhandler this block, also implemented the download proxy method, priority to execute block}#pragmaMark-nsurlsessiondownloaddelegate/** * * @param the path of the location temp file (downloaded file) after download*/- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) downloadtask Didfinishdownloadingtourl: (Nsurl *) location{//Location: The path to the temporary file (downloaded file)NSString*caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; //response.suggestedfilename: Recommended file name, generally consistent with server-side file nameNSString*file =[Caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; //cut or copy a temporary file caches folderNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //atpath: File path before clipping//Topath: The file path after clipping[Mgr MoveItemAtPath:location.path topath:file error:nil];}/** * Restore when download is called*/- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didresumeatoffset: (int64_t) Fileoffset expectedtotalbytes: (int64_t) expectedtotalbytes{}/** * it will be called (may be called multiple times) whenever a portion of the download is finished (can be invoked more than once) * * @param byteswritten How much this call wrote * @param totalbyteswritten cumulative How many lengths were written to the sandbox? * @param total length of totalbytesexpectedtowrite file*/- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didwritedata: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswritten Totalbytesexpectedtowrite: (int64_t) totalbytesexpectedtowrite{DoubleProgress = (Double) Totalbyteswritten/Totalbytesexpectedtowrite; NSLog (@"Download Progress---%f", progress);}#pragmaMark------------------------------------------------------------------/** Download task: Cannot see download progress*/- (void) downloadtask{//1. Get the Session objectNsurlsession *session =[Nsurlsession sharedsession]; //2. Create a download taskNsurl *url = [Nsurl urlwithstring:@"Http://localhost:****************test.mp4"]; Nsurlsessiondownloadtask*task = [Session Downloadtaskwithurl:url completionhandler:^ (Nsurl *location, Nsurlresponse *response, NSError *error) {        //Location: The path to the temporary file (downloaded file)NSString*caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; //response.suggestedfilename: Recommended file name, generally consistent with server-side file nameNSString *file =[Caches stringByAppendingPathComponent:response.suggestedFilename]; //cut or copy a temporary file caches folderNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //atpath: File path before clipping//Topath: The file path after clipping[Mgr MoveItemAtPath:location.path topath:file Error:nil];        }]; //3. Start a task[Task resume];}- (void) datatask{//1. Get the Session objectNsurlsession *session =[Nsurlsession sharedsession]; //2. Create a task, tasks//Nsurl *url = [Nsurl urlwithstring:@]http://***********/video "]; //Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData *data, Nsurlresponse *respons E, Nserror *error) {//nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil    ]; //NSLog (@ "----%@", dict); //    }];Nsurl*url = [Nsurl urlwithstring:@"Http://192.168.**********/login"]; //Create a requestNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod=@"POST"; //set the request bodyRequest. Httpbody = [@"username=123&pwd=123"datausingencoding:nsutf8stringencoding]; Nsurlsessiondatatask*task = [Session datataskwithrequest:request completionhandler:^ (NSData *data, Nsurlresponse *response, NSError *error) {Nsdictionary*dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; NSLog (@"----%@", Dict);        }]; //3. Start a task[Task resume];}@end

File Download (large file download)

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.