Ios-file Breakpoint Continuation

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="font-size: 16px;">* The mobile client is widely used for uploading and downloading when interacting with the Server.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* When we download the file, we can pause the download when clicking on the pause, click on the download can continue to download, how is this feature implemented?</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* How does the download progress bar appear?</span></strong></p></p><strong><strong><span style="font-size: 16px;">first of all, about the principle of breakpoint download:</span></strong></strong><p><p><strong><span style="font-size: 16px;">* First determine if the file to be downloaded exists in the local sandbox.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* If present, determine the length of the downloaded File.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Assuming that the file length is 500, we should start the download request starting from 500.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Click to pause the download, the link should be broken off.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Click Download again, according to download file length, continue to Download.</span></strong></p></p><strong><strong><span style="font-size: 16px;">The following is a combination of code to describe a specific implementation:</span></strong></strong><p><p><strong><span style="font-size: 16px;">* Suppose we want to download the path of the file on the Server: http://localhost:8080:down/xcode_6.dmg;</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* We will download the file method locally in the Sandbox. The file is Named: xcode.dmg.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Get the file save path code: (collapsed)</span></strong></p></p><pre><pre><span style="color: #008080;">1</span> -(nsstring *<span style="color: #000000;">) GetFilePath</span><span style="color: #008080;">2</span><span style="color: #000000;">{</span><span style="color: #008080;">3</span> nsstring *documentspath = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) [<span style="color: #800080;">0</span><span style="color: #000000;">];</span> <span style="color: #008080;">4</span> NSString *filepath = [documentspath stringbyappendingpathcomponent:<span style="color: #800000;">@ "</span><span style="color: #800000;">xcode.dmg</span><span style="color: #800000;">"</span> <span style="color: #000000;">];</span> <span style="color: #008080;">5</span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">filePath;</span> <span style="color: #008080;">6</span> }</pre></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">download File save path</span></span><p><p><strong><span style="font-size: 16px;">* Because the downloaded file is relatively large, we need to take the asynchronous request Network.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* There are four proxy methods commonly used in asynchronous request networks.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Initiate network request DOWNLOAD. (download start point is the last pause point, the last pause location method is to get the file properties, from the properties can see the length of the file, the request point is the content after that Length)</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Suppose to get the contents of a byte, you can use:</span></strong></p></p><p><p></p></p><p><p><strong><span style="font-size: 16px;">bytes = 0-499 Request the first 500 bytes of content</span></strong></p></p><p><p></p></p><p><p><strong><span style="font-size: 16px;">bytes = 500-content After 500 bytes is requested</span></strong></p></p><p><p></p></p><p><p><strong><span style="font-size: 16px;">bytes = 500-1000 Request a range of content in a resource</span></strong></p></p><p><p></p></p><p><p><strong><span style="font-size: 16px;">bytes = 0-1,499-500 Request for content between multiple scopes</span></strong></p></p><p><p><strong><span style="font-size: 16px;">* Progress bar past file download progress, You can use the number of bytes of downloaded files/files total Bytes.</span></strong></p></p><p><p><strong><span style="font-size: 16px;">The following is a specific code snippet with detailed comments:</span></strong></p></p><p><p></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller<nsurlconnectiondatadelegate>{ Progress bar variable, Show file download Progress iboutlet Uiprogressview *_progress; File processing, writes the downloaded file to the local sandbox nsfilehandle *_filehandle; URL connection, Point Solution pause, Cancel connection, Click download, start Download nsurlconnection *_connection; has received data size long _receivesize; The file has a total size of long _totalsize;} Download button Click Response Function-(ibaction) download: (id) sender;//pause button click Response Function-(ibaction) pause: (id) sender; @end</pre></pre><p><p></p></p><p><p></p></p><pre class="brush:objc;gutter:true;">-(ibaction) download: (id) sender{//file path NSString *string = @ "http://localhost:8080/down/xcode_6.dmg"; The path of the file is converted to URL nsurl *url = [nsurl urlwithstring:string]; Create a variable request nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; Determine if there is no such file under the sandbox path, Yes indicates that the file has been downloaded, No indicates that it has not been downloaded if ([[nsfilemanager defaultmanager] fileexistsatpath:[self getfilepath] = = Yes) {//depending on the properties of the file, get the amount of data previously downloaded nsdictionary *filedic = [[nsfilemanager defaultmanager]attributesof Itematpath:[self getfilepath] error:nil]; The length of the file is the amount of data that has been downloaded _receivesize = [filedic[nsfilesize] longlongvalue]; Set range, starting with the content after range//bytes = 0-499 request the first 500 bytes of content//bytes = 500-request 500 bytes after the content//bytes = 500-1000 Request a range of content in a resource//bytes = 0-1,499-500 request content between multiple scopes nsstring *bytes = [nsstring stringwithformat:@ "bytes=%lld-", _receivesize]; [request setvalue:bytes forhttpheaderfield:@ "Range"]; }//initiate Asynchronous request, File comparison method, use asynchronous Request _Connection = [nsurlconnection Connectionwithrequest:request delegate:self];} -(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) response{//analysis response header Nshttpurlresp Onse *httpresponse = (nshttpurlresponse *) response; Gets the response header, which is a dictionary nsdictionary *responseheader = httpresponse.allheaderfields; The total size of the received file equals the amount of data already downloaded, plus the amount of data downloaded _totalsize = _receivesize + [responseheader[@ "content-length"] longlongvalue]; Create a file management class Nsfilemanager *filemanager = [nsfilemanager defaultmanager]; If the file you want to download does not exist in the sandbox, create the file if ([filemanager fileexistsatpath:[self getfilepath]] = = No) {[filemanager Createfilea Tpath:[self getfilepath] Contents:nil attributes:nil]; }//write file according to path _filehandle = [nsfilehandle filehandleforwritingatpath:[self getfilepath];} -(void) connection: (nsurlconnection *) connection didreceivedata: (nsdata *) data{//file is larger, you need to write, write, you need to position the cursor to the end [_f Ilehandle seektoendoffile]; Writing data to a file [_filehandle writedata:data]; The latest received data size equals the data already in the file, plus the amount of data just downloaded _receivesize = _receivesize + data.length; Calculation Progress Float Jundu = (float) _receivesize/_totalsize; Use a progress bar to indicate progress _progress.progress = jundu;} -(void) connectiondidfinishloading: (nsurlconnection *) connection{//file complete transfer, close file management [_filehandle closefile];} -(ibaction) pause: (id) sender{//click pause, Cancel request Connection [_connection cancel]; _connection = nil;} -(nsstring *) getfilepath{nsstring *documentspath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, nsuserdomainmask, YES) [0]; NSString *filepath = [documentspath stringbyappendingpathcomponent:@ "xcode.dmg"]; Return filePath;}</pre><p><p></p></p><p><p>Ios-file Breakpoint Continuation</p></p></span>

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.