开发者

NSURLDownload resumeData nil

开发者 https://www.devze.com 2023-02-16 15:26 出处:网络
I\'ve written an download tool. It\'s working fine with NSURLDownload. But when it comes to resuming downloads I have some problems. The resumeData is always nil!

I've written an download tool. It's working fine with NSURLDownload. But when it comes to resuming downloads I have some problems. The resumeData is always nil! When I download the same files with jDownloader, stop the download and resume again, jDownloader resumes the download! So is is possible with this files and the server!

code:

- (void)startDownload {
    status = @"loading";
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    urlDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
    [urlDownload setDeletesFileUponFailure:NO];

    if (!urlDownload) {
        [self error:@"Failed starting download request!"];
    }
}

test:

- (IBAction)stopWithResumeOption:(id)sender {
    [urlDownload cancel];
    NSData *tempData = [urlDownload resumeData];

    NSLog(@"%@",tempData);
}

If I stop the download while loading with "stopWithResumeOption" I get always nil! Am I missing something?!

Edit:

What info does the resumeData return at all? If it is only the length of the already downloaded data or something, maybe I can create this resumeData by myself?! Anyone tried this before?

Edit 2:

Here is the response header of one file I'm trying to resume:

{
    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=0";
    Connection = "keep-alive";
    "Content-Length" = 43346723;
    "Content-Type" = "application/pdf";
    Date = "Sun, 13 Mar 2011 13:08:44 GMT";
    Etag = 1185n;
    Pragma = public;
    Server = dbws;
    "X-Robots-Tag" = "noindex,nofollow";
}

Even an ETag is provided by the server!

Is there a chance to do it this way:

- (IBAction) resume:(id)sender {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSString *authHeader = [NSString stringWithFormat:@"bytes=%llu-%llu",bytesReceived, totalBytesToLoad]; 
    [request addValue:authHeader forHTT开发者_如何学CPHeaderField:@"Range"];  

    urlDownload = [[NSURLDownload alloc] initWithRequest:request delegate:self];
}

But I'm not sure how to go on with this try...


The documentation says:

cancel

Cancels the receiver’s download and deletes the downloaded file.

Consider simulating the failure by disconnecting from the network instead. Don't forget to implement the delegate methods.

EDIT:

The documentation also says:

resumeData

Returns nil if the download is not able to be resumed.

Discussion

Resume data will only be returned if the protocol of the download as well as the server support resuming

Are you certain that the server supports resuming?

EDIT 2:

Make sure urlDownload is not nil in your stopWithResumeOption: method..

urlDownload is assigned in your startDownload method (how is it declared?)

Try:

urlDownload = [[[NSURLDownload alloc] initWithRequest:theRequest delegate:self] retain];

and remember to [urlDownload release] where appropriate.


I know this is old but hopefully this will save someone some time.

As of now, this is not mentioned in the documentation, but the resumeData property always returns nil unless the download is cancelled or fails.

Seems unfortunate since this makes it difficult to resume downloads should your app crash in the middle of a download.

0

精彩评论

暂无评论...
验证码 换一张
取 消