2

有没有人成功使用 AFNetworking 下载更大的文件?这是我现在正在使用的代码:

NSFileManager *fm = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dfwPath =  [documentsDirectory stringByAppendingPathComponent:@"sync_download.db"];

//Delete the existing one if our last sync was interrupted...
[fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:@"sync_download.db"] error:nil];

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"sync"   forKey:@"app"];
[parameters setObject:@"dl"     forKey:@"cmd"];
[parameters setObject:version   forKey:@"ver"];

[AFHTTPRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"application/bin"]];

AFMyClient *client = [AFMyClient sharedClient];

NSMutableURLRequest* rq = [client requestWithMethod:@"POST" path:@"myserver/ol.php" parameters:parameters];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:rq];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dfwPath append:NO];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    NSLog(@"The bytes download: %llu of %llu", totalBytesRead, totalBytesExpectedToRead);

    //float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));

}];

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

    NSLog(@"Finished downloading: %@", [operation responseString]);

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

    NSLog(@"Error downloading: %@",[error debugDescription]);
}];

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

发生的情况是文件被创建但它始终为零字节,即使正在下载的数量约为 10MB。永远不会达到“setDownloadProgressBlock”,但“setCompletionBlockWithSuccess”是。

这是我在发送下载数据之前在服务器上设置标头的方式:

        $data = file_get_contents($filename);
        header('Pragma: public'); 
        header('Content-Type: application/bin');
        header('Content-Disposition: attachment; filename="'.basename($filename).'";');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.strlen($data));       
        echo $data;

使用较旧的 ASIHttpRequest 库,这非常有效。因此,如果有人知道发生了什么,我将不胜感激。

4

2 回答 2

1

三件事:

首先,您可能需要在运行循环中安排输出流,以确保下载按预期进行。

其次,NSOperationQueue您在该方法中创建的 可能在操作完成之前被释放。相反,您应该将您的 HTTP 请求操作排入 AFHTTPClient 操作队列。

第三,不相关:你应该使用AFHTTPClient -HTTPRequestOperationWithRequest:success:failure:而不是手动创建一个AFHTTPRequestOperation类,并且做setCompletionBlockWithSuccess:failure:. 如果不使用客户端构造函数,您将错过已注册的操作逻辑和默认标头。

于 2012-10-30T06:34:21.173 回答
1

好吧,我想通了。事实证明,我并没有以最有效的方式从服务器发送数据,而且我还清理了我的 HTTP 标头,如下所示:

        $size = filesize($filename);
        header('Pragma: no-cache'); 
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filename).'";');
        header('Content-Transfer-Encoding: binary');
        header('Accept-Ranges: bytes');
        header('Cache-Control: max-age=604800');
        header("Content-Length: $size");        
        readfile($filename);
于 2012-10-30T15:25:56.720 回答