iOS implementation breakpoint Download

Source: Internet
Author: User

#import "ZYDownViewController.h"

@interface zydownviewcontroller ()

@end

@implementation Zydownviewcontroller

{

Nsmutabledata * buffer;

long long _totallength; // Total file size

long long _currentlength; // current Download size defaults to 0

Nsfilehandle * FILEHANDLE;

Nsurlconnection * CONN;

}

-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle *) nibbundleornil

{

Self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) Viewdidload

{

[Super Viewdidload];

Do any additional setup after loading the view from its nib.

NSString * pp=[self getpath];

filehandle =[nsfilehandle filehandleforwritingatpath:p p];

_progress. Progressviewstyle=1;

}

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

-(void) connection: (nsurlconnection *) connection didfailwitherror: (nserror *) error{

}

-(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) response{

Buffer=[[nsmutabledata alloc] init];

_totallength=[response expectedcontentlength] + _currentlength;

}

-(void) connection: (nsurlconnection *) connection didreceivedata: (nsdata *) data{

[Buffer appenddata:data];

if (FileHandle) {

//Navigate to the location where you want to write the file

[filehandle seektofileoffset: [filehandle seektoendoffile]];

[filehandle writedata:data];

}

_currentlength+=data. Length;

float f= (float) _currentlength/(float) _totallength;

_progress. Progress=f;

}

-(void) connectiondidfinishloading: (nsurlconnection *) connection{

Uialertview * alert=[[uialertview alloc] initwithtitle:@ " warm tip " message:@ " Download successful "delegate:self cancelbuttontitle:@" return "Otherbuttontitles:Nil, Nil];

[Alert show];

}

-(ibaction) Downbtnclick: (ID) sender {

Breakpoint Download

NSString * path=[self getpath]; // Get path

long long filesize=0;

Nsfilemanager * Filemg=[nsfilemanager defaultmanager];

//Get a Dictionary of file information

Nsdictionary *dic=[filemg attributesofitematpath:p ath error:nil];

Get File Size

NSNumber * contentlength=[dic objectforkey: nsfilesize];

//Get how much has been downloaded

Filesize=[contentlength Longlongvalue];

_currentlength=filesize;

//Get the size of the download file

nsurl * url=[nsurl urlwithstring: @ "HTTP://LOCALHOST:8080/SERVELET/3333.DMG"]; // Test Service Code

Nsmutableurlrequest * request=[nsmutableurlrequest requestwithurl: url];

[Request Sethttpmethod:@ "POST"];

//Set the bytes after the request because our requirements are based on the size of the obtained file .

[Request AddValue: [NSString stringWithFormat:@ "bytes=%qu-",_currentlength] Forhttpheaderfield:@ "Range"];

Conn=[nsurlconnection connectionwithrequest: Request delegate:self];

}

-(NSString *) getpath{

NSString * filepath=[nshomedirectory() stringbyappendingpathcomponent:@ "DOCUMENTS/TEST.DMG"];

Nsfilemanager * Filemg=[nsfilemanager defaultmanager];

if (![ FILEMG Fileexistsatpath:filepath]) {

[Filemg createfileatpath: FilePath contents:nil attributes:nil];

}

return filePath;

}

-(ibaction) Cancelbtnclick: (ID) sender {

[conn Cancel];

}

@end

iOS implementation breakpoint 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.