demo下载地址: http://download.csdn.net/detail/zaitianaoxiang/4334555
#import"newDownLoadViewController.h"
#import"ASIHTTPRequest.h"
#import"ASIFormDataRequest.h"
#import"ASINetworkQueue.h"
@implementation newDownLoadViewController
@synthesize progressIndicator;
@synthesize fileLocation;
@synthesize startButton;
@synthesize resumeButton;
@synthesize bandwidthUsed;
@synthesize urlTextField;
- (void)dealloc
{
[networkQueuerelease];
[superdealloc];
}
- (IBAction)initTheGong:(id)sender
{
networkQueue = [[ASINetworkQueuealloc] init];
NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(updateBandwidthUsageIndicator)userInfo:nilrepeats:YES];
timer =nil;
}
- (IBAction)URLFetchWithProgress:(id)sender
{
[startButtonsetTitle:@"Stop"forState:UIControlStateNormal];
[startButtonaddTarget:selfaction:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
NSString *tempFile = [[[[NSBundlemainBundle] bundlePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];
if ([[NSFileManagerdefaultManager] fileExistsAtPath:tempFile]) {
[[NSFileManagerdefaultManager] removeItemAtPath:tempFileerror:nil];
}
[selfresumeURLFetchWithProgress:self];
}
- (IBAction)stopURLFetchWithProgress:(id)sender
{
[startButtonsetTitle:@"Start"forState:UIControlStateNormal];
[startButtonaddTarget:selfaction:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
[networkQueuecancelAllOperations];
[resumeButtonsetEnabled:YES];
}
- (IBAction)resumeURLFetchWithProgress:(id)sender
{
[resumeButtonsetEnabled:NO];
[startButtonsetTitle:@"Stop"forState:UIControlStateNormal];
[startButtonaddTarget:selfaction:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
[networkQueuecancelAllOperations];
[networkQueuesetShowAccurateProgress:YES];
[networkQueuesetDownloadProgressDelegate:progressIndicator];
[networkQueuesetDelegate:self];
[networkQueuesetRequestDidFinishSelector:@selector(URLFetchWithProgressComplete:)];
ASIHTTPRequest *request = [[[ASIHTTPRequestalloc] initWithURL:[NSURLURLWithString:@"http://9991.net/blog/mp3/2.mp3"]]autorelease];
[requestsetDownloadDestinationPath:[[[[NSBundlemainBundle] bundlePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];
[requestsetTemporaryFileDownloadPath:[[[[NSBundlemainBundle] bundlePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.down"]];
[requestsetAllowResumeForFileDownloads:YES];
[networkQueueaddOperation:request];
[networkQueuego];
}
- (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request
{
if ([requesterror]) {
fileLocation.text=[NSStringstringWithFormat:@"An error occurred: %@",[[[requesterror] userInfo]objectForKey:@"Title"]];
}else {
fileLocation.text=[NSStringstringWithFormat:@"File downloaded to %@",[requestdownloadDestinationPath]];
}
[startButtonsetTitle:@"Start"forState:UIControlStateNormal];
[startButtonaddTarget:selfaction:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateBandwidthUsageIndicator
{
[bandwidthUsedsetText:[NSStringstringWithFormat:@"%luKB / second",[ASIHTTPRequestaverageBandwidthUsedPerSecond]/1024]];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end