iOS Development Web Chapter-File download (two · reasonable)
One, side download, side write
1. Ideas
Append the downloaded data to the end of the file until all the data has been downloaded.
1. When the server is connected, create an empty file into the sandbox Nsfilemanager (file management Class)
2. Create a file handle to write data
3. After receiving the data returned by the server, it writes to the empty file created, but cannot use WriteToFile (overwrite)
3.1 Move to the end of the file
3.2 Write data from the position currently being moved
4. When the server data is loaded, close the connection and no longer enter the data in the file
Second, code example
1 //2 //YYVIEWCONTROLLER.M3 //01-Downloading of files (unreasonable)4 //5 //Created by Apple on 14-6-30.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten One @interfaceYyviewcontroller () A@property (nonatomic,strong) Nsmutabledata *FileData; -@property (nonatomic,strong) Nsfilehandle *Writehandle; --(ibaction) star; the - @end - - @implementationYyviewcontroller + -- (void) Viewdidload + { A [Super Viewdidload]; at } - --(ibaction) star { - //Create a download path - -Nsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/resources/videosres.zip"]; in - //Create a request toNsurlrequest *request=[Nsurlrequest Requestwithurl:url]; + - //send a request (using proxy method) theNsurlconnection *connt=[nsurlconnection Connectionwithrequest:requestDelegate: self]; * [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 save 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]; $ } - - /* - * Called when data is received from the server (may be called multiple times, only partial data is passed at a time) A */ +-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data the { - //1.1 points to receive data. $NSLog (@"receive data from the server! ---%d", data.length); the //writes data to the created empty file, but cannot use WriteToFile (overwrites) the //move to the end of a file the [Self.writehandle seektoendoffile]; the //writes data from the position currently being moved - [Self.writehandle Writedata:data]; in } the the /* About * When the server's data is loaded, it will be called the */ the-(void) Connectiondidfinishloading: (Nsurlconnection *) Connection the { +NSLog (@"Download Complete"); - //closes the connection and no longer enters data in the file the [Self.writehandle CloseFile];Bayi } the /* the * Request error (failure) when called (Request timeout \ network \ No net \, usually refers to client error) - */ --(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error the { the } the @end
Note the point :
(1) Create a File store path (write to sandbox)
NSString *caches=[nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastObject];
NSString *filepath=[caches stringbyappendingpathcomponent:@ "Video.zip"];
(2) Create an empty folder (use of the Nsfilemanager class)
Nsfilemanager *mgr=[nsfilemanager Defaultmanager];
(3) Create a file handle to write data
Self.writehandle=[nsfilehandle Filehandleforwritingatpath:filepath];
(4) Write data to the created empty file, but cannot use WriteToFile (overwrite)
Move the handle to the tail of the file [Self.writehandle Seektoendoffile];
(5) When the download is complete, close the connection
[Self.writehandle CloseFile];