First, to achieve the download file progress control
1. Code examples
1 #import "YYViewController.h"2 3 @interfaceYyviewcontroller ()4@property (nonatomic,strong) Nsmutabledata *FileData;5@property (nonatomic,strong) Nsfilehandle *Writehandle;6@property (nonatomic,assign)Long Longcurrentlength;7@property (nonatomic,assign)Long Longsumlength;8@property (Weak, nonatomic) Iboutlet Uiprogressview *progress;9 Ten-(ibaction) star; One A @end - - @implementationYyviewcontroller the -- (void) Viewdidload - { - [Super Viewdidload]; + } - +-(ibaction) star { A at - //Create a download path - -Nsurl *url=[nsurl urlwithstring:@"Http://192.168.0.109:8080/MJServer/resources/video.zip"]; - - //Create a request inNsurlrequest *request=[Nsurlrequest Requestwithurl:url]; - to //send a request (using proxy method) + //nsurlconnection *connt= -[Nsurlconnection Connectionwithrequest:requestDelegate: self]; the //[connt start]; * $ }Panax Notoginseng - #pragmaMark-nsurlconnectiondatadelegate Proxy method the /* + * When the response to the server is received (the server is connected) , the A */ the-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response + { - //1. Create a file storage path $NSString *caches=[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; $NSString *filepath=[caches stringbyappendingpathcomponent:@"Video.zip"]; - - the - //2. Create an empty file into the sandboxWuyiNsfilemanager *mgr=[Nsfilemanager Defaultmanager]; the //the size you just created is o bytes - [Mgr Createfileatpath:filepath contents:nil Attributes:nil]; Wu - //3. Create a file handle to write data Aboutself.writehandle=[Nsfilehandle Filehandleforwritingatpath:filepath]; $ - //4. Get the full length of the file -Self.sumlength=response.expectedcontentlength; - } A + /* the * Called when data is received from the server (may be called multiple times, only partial data is passed at a time) - */ $-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data the { the //accumulate the received data theself.currentlength+=data.length; the - //calculates the current progress (converted to double type) in DoubleProgress= (Double) self.currentlength/self.sumlength; theself.progress.progress=progress; the About //1.1 points to receive data. theNSLog (@"receive data from the server! ---%d", data.length); the the //writes data to the created empty file, but cannot use WriteToFile (overwrites) + //move to the end of a file - [Self.writehandle seektoendoffile]; the //writes data from the position currently being movedBayi [Self.writehandle Writedata:data]; the } the - /* - * When the server's data is loaded, it will be called the */ the-(void) Connectiondidfinishloading: (Nsurlconnection *) Connection the { theNSLog (@"Download Complete"); - the //closes the connection and no longer enters data in the file the [Self.writehandle CloseFile]; the //destroyed94self.writehandle=Nil; the the //after the download is complete, clear the progress theSelf.currentlength=0;98Self.sumlength=0; About - 101 }102 /*103 * Request error (failure) when called (Request timeout \ network \ No net \, usually refers to client error)104 */ the-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error106 {107 }108 @end
2. Display
Simulator Display:
Print View: