使用MKNetworkKit下载大文件会导致出现空文件

时间:2013-10-08 18:48:18

标签: ios objective-c mknetworkkit mknetworkengine

我尝试使用MKNetworkKit(iOS7)下载大文件。目前使用以下代码:

- (MKNetworkOperation*)downloadFile: (NSString*)url :(NSString*)output {
     NSString* fullPath = [documentsPath stringByAppendingPathComponent:output];
     MKNetworkOperation *op = [self operationWithURLString:url params:nil httpMethod:@"GET"];
     [op addDownloadStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];
     return op;
 }

然后我从另一个类这样称呼它:

Tools *dl = [[Tools alloc]init];
    MKNetworkOperation* op = [dl downloadFile:someURL :[documentsPath stringByAppendingPathComponent:@"someFile.txt"]];
    [op onDownloadProgressChanged:^(double prog) {
        progressView.progress = prog;
        progressLabel.text = [NSString stringWithFormat:@"%.1f%%",prog*100];
    }];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation){
        //Downloaded file completed here
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error){
        NSLog(@"%@", error);
    }];
    [dl enqueueOperation: op];

下载完成后,文件名显示在Documents文件夹中,但文件为Zero Bytes并为空。 MKNetworkKit不喜欢下载大文件吗?

1 个答案:

答案 0 :(得分:1)

我发现错误,似乎我不小心把输出文件放在了错误的地方。由于文件夹不存在,MKNetworkKit没有将其写入文件夹。指定正确的路径解决了它。