gpt4 book ai didi

ios - 使用附加 :YES with operation. outputStream AFHTTPRequestOperation 不起作用

转载 作者:行者123 更新时间:2023-11-28 22:19:08 24 4
gpt4 key购买 nike

我正在尝试下载一个文本文件,如果此下载暂停,我将尝试附加文件的其余部分。

问题是我开始从正确的位置下载其余部分,但文件没有附加它会覆盖以前的文件。

这是我的源代码: +(无效)下载文件:(NSManagedObject *)文件{

NSString *url = [file valueForKey:@"path"];

NSString *fileName = [[file valueForKey:@"path"] lastPathComponent]; // TODO chnage to file ID (it will be good to add id before file name)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

BOOL append = NO;
unsigned long long size = 0;
BOOL resumeOperation = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
resumeOperation = YES;
NSError *error = nil;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
size = [[dict objectForKey:NSFileSize] longLongValue];

if (!error) {
error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];

if (error) {
NSLog(@"Error removing old file: %@", [error localizedDescription]);
}

NSString *val = [NSString stringWithFormat:@"bytes=%lld-", size];
append = YES;
[request setValue:val forHTTPHeaderField:@"Range"];
}

}

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;


operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];


//gives more 10 minuts when the app is minimmized
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
[operation pause];
}];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
totalBytesRead += size;
float percent = ((float)totalBytesRead) / (totalBytesExpectedToRead + size);

// TODO update database with bytes amount
NSLog(@"%.2f%% - [%d]", percent * 100, [[CoreDataManager getQueue] count]);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"File downloaded succesffully!");
[self reportEndDownload:file withStatusCode:[NSNumber numberWithInt:DOWNLAOD_STATUS_COMPLETE]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// NSLog(@"Error while downloading file: %@", [error localizedDescription]);
// ? ask about server corrupted files - here is a loop -----> Itay OK
[self reportEndDownload:file withStatusCode:[NSNumber numberWithInt:DOWNLOAF_STATUS_CONNECTION_ERROR]];
}];

if (resumeOperation ) {
[operation resume];
}else{
[operation start];
}


}

最佳答案

您可以使用 AFDownloadRequestOperation 来执行此操作。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
}];
[operations addObject:operation];

关于ios - 使用附加 :YES with operation. outputStream AFHTTPRequestOperation 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20881923/

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