使用ASIHTTPRequest继续下载文件会出错

时间:2011-11-25 11:35:10

标签: iphone objective-c xcode4 download

我使用ASIHTTPRequest恢复文件下载会产生如下错误,并在底部给出简历代码:

Error Domain=ASIHTTPRequestErrorDomain Code=8 "Decompression of /Users/xxxx/Library/Application Support/iPhone Simulator/4.3.2/Applications/6E0D8E0F-08FD-440C-82F6-8E39E219884E/Documents/myPdf.pdf.download failed with code -3" UserInfo=0x4c6a8e0 {NSLocalizedDescription=Decompression of /Users/xxxx/Library/Application Support/iPhone Simulator/4.3.2/Applications/6E0D8E0F-08FD-440C-82F6-8E39E219884E/Documents/myPdf.pdf.download failed with code -3}

开始下载如下:

-(IBAction)startDownload:(id)sender
{
    NSURL *url = [NSURL URLWithString:self.sourcePath];
    ASIHTTPRequest *req =[[ASIHTTPRequest alloc] initWithURL:url];
        [request setDownloadDestinationPath:self.destinationPath];
        // This file has part of the download in it already
    [request setTemporaryFileDownloadPath:self.temporaryPath];
    [req setDownloadProgressDelegate:self];
    [req setDelegate:self];
    [req startAsynchronous];
    self.request = req;

}

和暂停下载如下:

-(IBAction)pauseDownload:(id)sender
{
    // Cancels an asynchronous request
    [request cancel];

    // Cancels an asynchronous request, clearing all delegates and blocks first
   // [request clearDelegatesAndCancel];

}

和简历下载如下:

- (IBAction)resumeDownload:(id)sender
{
    NSURL *url = [NSURL URLWithString:
                  self.sourcePath];
    ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSError *err;
    NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:destinationPath error:&err];
    NSLog(@"file Dict: %@", fileDict);

    NSLog(@"size: %@",[fileDict valueForKey:NSFileSize]);
    NSInteger nSize =[[fileDict valueForKey:NSFileSize] intValue];

   // unsigned long long int size1 = [[fileDict valueForKey:NSFileSize] intValue];

    NSString *size = [NSString stringWithFormat:@"bytes=%d", nSize];
    NSLog(@"file size: %@",size);

    [dict setValue:size forKey:@"Range"];
    [request1 setRequestHeaders:dict];

    // NSString *downloadPath = @"/Users/ben/Desktop/my_work_in_progress.txt";

    // The full file will be moved here if and when the request completes successfully
    [request1 setDownloadDestinationPath:self.destinationPath];

    // This file has part of the download in it already
    [request1 setDownloadProgressDelegate:self];
    [request1 setDelegate:self];
    [request1 setTemporaryFileDownloadPath:self.temporaryPath];
    [request1 setAllowResumeForFileDownloads:YES];
    [request1 startAsynchronous];
    self.request = request1;
    //The whole file should be here now.
 //   NSString *theContent = [NSString stringWithContentsOfFile:downloadPath];
}

我将“Range”HTTP标头字段设置为相应的文件大小。服务器上的同一文件支持下载暂停,恢复应用http://itunes.apple.com/us/app/download-manager-pro-lite/id348573579?mt=8 如何实现恢复下载 提前谢谢。