2016-03-04 109 views
0

我正在使用AFNetworking 3.0下载文件,它似乎是做下载部分罚款,但我找不到文件后。我正在使用下面的代码。在下载任务中,如果我在目标块中设置了断点,看起来好像目标路径和下载目标路径是正确的,并且实际上在targetPath指向tmp文件夹中存在的tmp文件并包含正确下载数据。但是,如果我然后在完成处理程序块中找到断点,则tmp文件已消失,并且没有文件位于我的下载目标路径指向的位置。AFNetworking下载文件丢失

我错过了一个步骤吗?我是否必须自己移动这个文件,还是AFNetworking应该照顾的东西?从上面的NSLogs

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]; 

输出:

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 

回答

2

哎呀,那是我傻的。答案是在那些日志中面对我。
临时文件的文件路径以file://开头,而我的下载目标路径不是。答案是改变

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

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

这会给我下载的文件发送到

一个有效的文件路径