【问题标题】:AFNetworking downloaded file missingAFNetworking 下载的文件丢失
【发布时间】:2016-03-04 02:47:56
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: ios afnetworking afnetworking-3


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多