gpt4 book ai didi

AFNetworking - 使用 AFHTTPRequestOperation 下载文件?

转载 作者:行者123 更新时间:2023-12-03 14:27:47 26 4
gpt4 key购买 nike

伙计,被这个难住了。尝试使用 AFNetworking 下载文件我能做的最好的事情就是在 downloadProgressBlock 中捕获进度字节。我已经尝试了许多不同的 AFNetworking 分支,现在我又回到了最新版本。看起来有一次 AFHTTPRequestOperation 被修改为还包括 NSURLResponse ,但在最新版本中已经消失了。而且,根据下面的代码,永远不会调用“成功”块。我得到下载字节的日志,然后 ^complete 被调用。成功和错误永远不会被调用。

任何关于这方面的指导都会很棒。我不知道返回数据的位置以及如何让它在其上使用 NSFileManager?我需要下载文件,而不是为图像等写入流。

编辑:我也尝试通过覆盖 - (void)connection:didReceiveData 按照文档中的建议来捕获数据,但没有做任何事情。

//网址是 http://mydomain.com/somezip.zip

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]];

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) {

NSLog(@"hey, some success!");

} failure:^(NSHTTPURLResponse *response, NSError *error) {

NSLog(@"%@", error);

}];


[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {

// callback is sent in and works great. Just outputting to the console at the moment
SEL callme = NSSelectorFromString(callback);
[sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO];

}];

[operation setCompletionBlock:^{
NSLog(@"operation complete");
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

最佳答案

您可以简单地使用 AFNetworking 中包含的此功能,甚至将其保存到磁盘 -
请注意 operation.outputStream :

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

// Deal with failure
}];

编辑 : missed
[operation start]; 

编辑 : ...or if you use an AFHTTPClient * apiClient :
// [apiClient enqueueHTTPRequestOperation:operation];

关于AFNetworking - 使用 AFHTTPRequestOperation 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004808/

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