如何取消AFdownloadrequest *** 作并且不保留以前的进度(删除临时文件)iOS

如何取消AFdownloadrequest *** 作并且不保留以前的进度(删除临时文件)iOS,第1张

概述我正在从服务器下载文件. 我能够成功下载文件. 但每当我取消下载请求(中间) *** 作并重新开始下载. 下载从先前的进度开始(而不是从零开始). 但我想从零开始重新下载. 设置operation.deleteTempFileOnCancel = YES;也没有帮助 当我取消之间的下载时,在给定的目标路径上没有创建文件 operation.tempPath is returning null (not a 我正在从服务器下载文件.
我能够成功下载文件.
但每当我取消下载请求(中间) *** 作并重新开始下载.
下载从先前的进度开始(而不是从零开始).
但我想从零开始重新下载.

设置operation.deleteTempfileOnCancel = YES;也没有帮助

当我取消之间的下载时,在给定的目标路径上没有创建文件

operation.tempPath is returning null (not able to delete temporary file)

我使用以下代码

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.downloadUrl]];     operation = [[AFDownloadRequestoperation alloc] initWithRequest:request targetPath:path shouldResume:YES];     operation.deleteTempfileOnCancel = YES;            // download operation progressive block            [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestoperation *operation,NSInteger bytesRead,long long totalBytesRead,long long totalBytesExpected,long long totalBytesReadForfile,long long totalBytesExpectedToReadForfile)             {                 // calculate current download progress                 }             }];            // download operation completion block            [operation setCompletionBlockWithSuccess:^(AFhttpRequestoperation *operation,ID responSEObject)             {             }             failure:^(AFhttpRequestoperation *operation,NSError *error) {                 // operation Failed             }];           [operation start];

如果在中间取消下载然后再次启动,我想从零开始下载.我使用的是iOS 7

解决方法 好吧,我刚刚找到了解决方法.我会在这里发布,以防其他人有同样的问题.

在此行之后,您可以检查是否实际创建了tempPath

operation = [[AFDownloadRequestoperation alloc] initWithRequest:request targetPath:path shouldResume:YES];NSLog("%@",operation.tempPath);

然后将该路径添加到字典中

Nsstring *tempPath = _operation.tempPath;[self.tempPathDict setobject:tempPath forKey:your_key];

当您停止 *** 作时,您可以调用此代码

Nsstring *tempPath = [self.tempPathDict objectForKey:your_key];// Cancel the operation you want[operation cancel];// Remove the tempfile NSError *error = [[NSError alloc] init];[[NSfileManager defaultManager] removeItemAtPath:tempPath error:&error];// And last remove the path from the dictionary[self.tempPathDict removeObjectForKey:your_key];
总结

以上是内存溢出为你收集整理的如何取消AFdownloadrequest *** 作并且不保留以前的进度(删除临时文件)iOS全部内容,希望文章能够帮你解决如何取消AFdownloadrequest *** 作并且不保留以前的进度(删除临时文件)iOS所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1023354.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存