gpt4 book ai didi

iphone - ASIHTTPRequest 多个下载控制,如暂停、恢复

转载 作者:行者123 更新时间:2023-11-29 04:15:43 25 4
gpt4 key购买 nike

我正在使用 ASIHTTPRequest 从后台 URL 下载视频文件。

我正在显示带有进度条和百分比的下载,我希望用户可以控制下载,例如暂停和恢复。

下面是代码:

 -(void)Initiate_Download:(NSString*)urlStr contentID:(NSString*)cid progressBar:(UIProgressView*)progressBar
{
NSLog(@"Initiate_Download for cid:%@",cid);

urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];

NSString *fileName = [NSString stringWithFormat:@"%@.mp4",cid];
NSString *destinationPath = [[self VideoDownloadFolderPath]stringByAppendingPathComponent:fileName];

[request setDownloadDestinationPath:destinationPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
[request setDelegate:self];

NSDictionary *rqstDict = [NSDictionary dictionaryWithObjectsAndKeys:cid,@"cid",urlStr,@"url", nil];
[request setUserInfo:rqstDict];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
}

//Delegate
- (void)requestStarted:(ASIHTTPRequest *)request1
{
//some code
}
- (void)request:(ASIHTTPRequest *)request1 didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
//some code
}
- (void)requestFinished:(ASIHTTPRequest *)request1
{
//some code
}
- (void)requestFailed:(ASIHTTPRequest *)request1
{
//some code
}

最佳答案

您需要保存每个请求的 URL 和目标路径,并使用代码暂停请求:-

[request Cancel];

要恢复请求,您需要创建另一个具有相同 URL 和目标路径的请求。例如:-

    ASIHTTPRequest *requestToResume = [ASIHTTPRequest requestWithURL:url];
[requestToResume setTemporaryFileDownloadPath:tempfilePath];
[requestToResume setDownloadDestinationPath:filePath];
[requestToResume setDelegate:self];
[requestToResume setDownloadProgressDelegate:self];
[requestToResume setUserInfo:dictInfo];

// This file has part of the download in it already
[requestToResume setAllowResumeForFileDownloads:YES];
[requestToResume setDidFinishSelector:@selector(requestDone:)];
[requestToResume setDidFailSelector:@selector(requestWentWrong:)];
[requestToResume startAsynchronous];

在上面的代码中,我们从字典中获取歌曲的 url,该字典被设置为请求的 userInfo,现在我们获取这些详细信息以恢复请求。当我们恢复请求时,文件就会从暂停的地方开始下载,这样就解决了恢复文件下载的目的。

关于iphone - ASIHTTPRequest 多个下载控制,如暂停、恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13757710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com