AFNetworking下载的文件丢失了

时间:2016-03-04 02:47:56

标签: ios afnetworking afnetworking-3

我使用AFNetworking 3.0下载文件,似乎下载部分正常,但我之后无法找到该文件。

我使用下面的代码。在下载任务中,如果我在目标块中设置断点,似乎目标路径和下载目标路径是正确的,实际上在targetPath指向tmp文件夹中的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

1 个答案:

答案 0 :(得分:3)

唉,这对我来说很傻。答案就是在那些日志中盯着我 临时文件的文件路径以file://开头,而我的下载目标路径则不是。答案是改变

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

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

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