Search code examples
iphoneobjective-casihttprequest

Resumed a Download Using ASIHTTPRequest


i am downloading a file from the web. When i resumed the downloading of the file, it restart again from the starting point and not from the point where i have paused the download. i have saved the values of the downloading file at the point i paused the download. but now i am unable how to resumed the download of the file where i have paused. I am using this code for downloading a new file

      url = [NSURL URLWithString:_item.link];   
    _receivedBytes = 0;
    _speed = 0;
    _lastTime = [[NSDate date] timeIntervalSince1970];
    _connection = [ASIWebPageRequest requestWithURL:url];
    [_connection setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
    [_connection setDelegate: self];
    [_connection setDownloadProgressDelegate:self];
    [_connection setDownloadCache:[ASIDownloadCache sharedCache]];
    [_connection setDownloadDestinationPath: _item.path];
    [_connection startAsynchronous];

Solution

  • According to the ASIHTTPRequest docs resuming interrupted downloads works only if you are downloading directly into a file.

    I assume that you download into memory (because you are using _receivedBytes).

    If you are storing the downloaded file into the devices filesystem anyway, you could download it directly into the filesystem by using:

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setTemporaryFileDownloadPath:@"/path/to/tempfolder/yourfile.jpg.download"];
    [request setDownloadDestinationPath:@"/path/to/final/destination/yourfile.jpg"];
    

    Then you can enable the resuming of interrupted downloads by setting:

    [request setAllowResumeForFileDownloads:YES];
    

    You'll find more details here