iOS Development-File Download (2 reasonable)

Source: Internet
Author: User

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.M 3//01-File Download (unreasonable) 4//5//Created by Apple on 14-6-30. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (nonatomic,strong) Nsmutabledata *fil Edata;13 @property (Nonatomic,strong) nsfilehandle *writehandle;14-(ibaction) star;15 @end17 @implementation     YYViewController19-(void) viewDidLoad21 {[Super viewdidload];23}24-(ibaction) Star {26//create download Path 27 Nsurl *url=[nsurl urlwithstring:@ "Http://192.168.1.53:8080/MJServer/resources/videosres.zip"];29 30//Create one please For nsurlrequest *request=[nsurlrequest requestwithurl:url];32 33//Send request (using proxy method) Nsurlconnection *connt =[nsurlconnection connectionwithrequest:request delegate:self];35 [connt start];36}37 #pragma mark-nsurlconnectio Ndatadelegate Proxy Method 39/*40 * When receiving a response from the server (connected to the server) will call the */42-(void) connection: (Nsurlconnection *) connection Didreceiveresponse: (NsurLresponse *) response43 {44//1. create file Save path NSString *caches=[nssearchpathfordirectoriesindomains (Nscachesdirectory,     Nsuserdomainmask, YES) lastobject];46 nsstring *filepath=[caches stringbyappendingpathcomponent:@ "Video.zip"];47 48 49 50//2. Create an empty file into the sandbox in Nsfilemanager *mgr=[nsfilemanager defaultmanager];52//Just created the size is O byte [mg  R Createfileatpath:filepath Contents:nil attributes:nil];54 55//3. Create a file handle that writes data. Self.writehandle=[nsfilehandle filehandleforwritingatpath:filepath];57}58 59/*60 * Called when data is received from the server (may be called multiple times, only part of the data is delivered at a time). */62-(void) connection: ( Nsurlconnection *) connection didreceivedata: (NSData *) data63 {64//1.1 points receive data. NSLog (@ "received data from the server! ---%d ", data.length); 66//writes data to the created empty file, but cannot use WriteToFile (overwrites) 67//To move to the tail of the file [Self.writehandle Seektoendoffi le];69//From the current moving position, write data [Self.writehandle writedata:data];71}72 73/*74 * When the server's data is loaded it will call the */76-(void) Connec Tiondidfinishloading: (nsurlconnection *) Connection77 {NSLog (@ "Download complete"), 79//close connection, no longer input data in file [Self.writehandle closefile];81}82/*83 * Request error (failed) call (Request timed out \ Broken network \ No net \, generally referred to as client error] */85-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error86 {87}88 @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];

Tags: iOS development, network chapter

iOS Development-File Download (2 reasonable)

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.