#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