1

我正在使用 AFNetworking 3.0 下载文件,下载部分似乎很好,但之后我找不到文件。

我正在使用下面的代码。在下载任务中,如果我在目标块中设置断点,似乎目标路径和下载目标路径是正确的,实际上指向的targetPathtmp 文件夹中存在并包含正确的 tmp 文件下载的数据。但是,如果我随后在完成处理程序块中命中断点,则 tmp 文件已消失,并且没有文件指向我的下载目标路径。

我错过了一步吗?我必须自己移动这个文件,还是 AFNetworking 应该处理的事情?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

self.theRequest = [[AFHTTPRequestSerializer serializer] 
        requestWithMethod:self.RequestMethod            //@"POST"
                URLString:requestURL.absoluteString     //url to my API
               parameters:self.Parameters               //params being sent to API
                    error:nil];

//headers in this example:
//"Content-Type" = "application/json"
//"X-Requested-With" = XMLHttpRequest
//token = "<API TOKEN>";
for (id key in headers) {
    [self.theRequest setValue:headers[key] forHTTPHeaderField:key];
}

self.theRequest.timeoutInterval = 60 * 100;

NSURLSessionDownloadTask * downloadTask =
        [manager downloadTaskWithRequest:self.theRequest
                                progress:^(NSProgress * _Nonnull downloadProgress) {
                                    if(self.DownloadProgressHandler)
                                    self.DownloadProgressHandler(downloadProgress.fractionCompleted);
                                }
                             destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                                    NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];
                                    NSLog(@"%@",[targetPath absoluteString]);
                                    NSLog(@"%@",[url absoluteString]);
                                    return url;
                                }
                       completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
                                    [self RequestCompleteWithResponse:response responseObject:[[filePath absoluteString] dataUsingEncoding:NSUTF8StringEncoding] error:error];
                                }];
self.theTask = downloadTask;

[self.theTask resume];

上面 NSLogs 的输出:

2016-03-04 13:43:44.412 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 648 $ file:///Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/tmp/CFNetworkDownload_7VGArX.tmp
2016-03-04 13:43:44.425 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 649 $ /Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/Documents/9dfd86c2-458e-4725-a184-5fcd87f94dbd.inspect
4

1 回答 1

3

啊,那是我的愚蠢。答案就在那些日志中盯着我的脸。
临时文件的文件路径以 file:// 开头,而我的下载目标路径不是。答案是改变

NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];

NSURL *url = [NSURL fileURLWithPath:self.downloadDestinationPath];

这将为我提供一个有效的文件路径来将下载的文件发送到

于 2016-03-04T03:03:19.547 回答