zoukankan      html  css  js  c++  java
  • ASIHTTPRequest实现断点下载

    - (IBAction)URLFetchWithProgress:(id)sender
    
    {
    
    [startButton setTitle:@"Stop" forState:UIControlStateNormal];
    
    [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
    
    NSString*tempFile = [[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:tempFile]) {
    
    [[NSFileManager defaultManager] removeItemAtPath:tempFile error:nil];
    
    }
    
    [self resumeURLFetchWithProgress:self];
    
    }
    
    - (IBAction)stopURLFetchWithProgress:(id)sender
    
    {
    
    networkQueue = [[ASINetworkQueue alloc] init];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(updateBandwidthUsageIndicator) userInfo:nil repeats:YES];
    
    timer = nil;
    
    [startButton setTitle:@"Stop" forState:UIControlStateNormal];
    
    [startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
    
    [networkQueue cancelAllOperations];
    
    [resumeButton setEnabled:YES];
    
    }
    
    - (IBAction)resumeURLFetchWithProgress:(id)sender 
    
    {
    
    [resumeButton setEnabled:NO];
    
    [startButton setTitle:@"Start" forState:UIControlStateNormal];
    
     [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
    
    [networkQueue cancelAllOperations];
    
    [networkQueue setShowAccurateProgress:YES];
    
    [networkQueue setDownloadProgressDelegate:progressIndicator];
    
    [networkQueue setDelegate:self];
    
    [networkQueue setRequestDidFinishSelector:@selector(URLFetchWithProgressComplete:)];
    
    ASIHTTPRequest*request=[[[ASIHTTPRequest alloc] initWithURL:[NSURLURLWithString:@"http://9991.net/blog/mp3/2.mp3"]] autorelease];
    
    [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath]
    
    stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];
    
    [request setTemporaryFileDownloadPath:[[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.down"]];
    
    [request setAllowResumeForFileDownloads:YES];
    
    [networkQueue addOperation:request];
    
    [networkQueue go];
    
    }
    
    - (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request
    
    {
    
    if ([request error]) {
    
    fileLocation.text=[NSString stringWithFormat:@"An error occurred:%@",[[[requesterror] userInfo] objectForKey:@"Title"]];
    
    } else {
    
    fileLocation.text=[NSString stringWithFormat:@"File downloaded to %@",[requestdownloadDestinationPath]];
    
    }
    
    [startButton setTitle:@"Start" forState:UIControlStateNormal];
    
    [startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];
    
    }
    
    - (IBAction)throttleBand(id)sender
    
    {
    
    if ([(UIButton *)sender state] ==YES) {
    
    [ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];
    
    } else {
    
    [ASIHTTPRequest setMaxBandwidthPerSecond:];
    
    }
    
    }
  • 相关阅读:
    01pxc集群的部署
    Python简单爬虫获取岗位招聘人数
    nginx跨域问题记录
    自动备份远程mongodb数据库并拉取到本地
    ftp和mysql数据库结合使用
    flask蓝图的简单使用
    apache无法启动报错No space left on device
    简单的接口测试类和测试生成报告工具HTMLTestRunner.py
    简单python接口测试编写和django开发环境的搭建
    Fiddler模拟自动响应数据
  • 原文地址:https://www.cnblogs.com/jinlianglu/p/6511403.html
Copyright © 2011-2022 走看看