ios - AFNetworking 文件下载错误Error Domain=NSCocoaErrorDomain Code=260

标签 ios afnetworking nsfilemanager nsurlsessiondownloadtask nsurlsessionconfiguration

我正在使用 AFNetworking 3.0.4 进行视频文件下载和进度报告。该文件似乎已成功下载,但是当我保存此文件并尝试获取文件属性时,出现以下错误。

Error Domain=NSCocoaErrorDomain Code=260 "The file “7598-Y2TKGNGSEG93SIT.mp4” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/919E2138-6327-4651-89A2-FCA951FDC128/Documents/video/7598-Y2TKGNGSEG93SIT.mp4, NSUnderlyingError=0x15e995970 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

我正在使用这一行来获取文件属性。

 NSError *fileError;
      NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[filePath absoluteString] error:&fileError];

这里的 fileAttributes 字典是 nil,它在 fileError 变量中给出了上述错误。

这是我的完整代码

- (void)downloadFile:(MyObject *)myObj progress:(void (^)(float))progress success:(void (^)(NSString *filename))success failure:(void (^)())failure {


  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

  NSURL *URL = [NSURL URLWithString: myObj.downloadUrl];
  NSURLRequest *request = [NSURLRequest requestWithURL:URL];

  NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"Completed : %f", downloadProgress.fractionCompleted);
    dispatch_async(dispatch_get_main_queue(), ^{
      [self progressBlocksForUrl: myObj.archiveUrl withProgress:downloadProgress.fractionCompleted];
    });
   } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    NSLog(@"Destination");
    NSURL *url = [NSURL URLWithString:myObj.archiveUrl];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *videoPath = [NSString stringWithFormat:@"/%@/%@", @"video", [url lastPathComponent]];
    NSString *fullPath = [[paths firstObject] stringByAppendingPathComponent:videoPath];
    return  [NSURL URLWithString:fullPath];
   } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {


     NSHTTPURLResponse * myresponse = (NSHTTPURLResponse *)response;
     NSLog(@"Video downloaded, headers: %@", [myresponse.allHeaderFields description]);

    if(error) {
      NSLog(@"Error downloading video from %@: %@", myObj.archiveUrl, [error description]);
      [self failureBlockForUrl:myObj.archiveUrl];
    } else{

      NSError *fileError;
      NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[filePath absoluteString] error:&fileError];

      if (fileError) {
        NSLog(@"ERR: %@", [fileError description]);
        [self failureBlockForUrl:myObj.archiveUrl];
      } else {
        NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
        long long fileSize = [fileSizeNumber longLongValue];
        NSLog(@"File %@ with size %lld downloaded!", filePath, fileSize);
        [self completionBlocksForUrl:myObj.archiveUrl withFilename:[filePath absoluteString]];
      }
    }

  }];


  [downloadTask resume];
}

有人可以指导我我在这里做错了什么吗?任何帮助将不胜感激。谢谢

最佳答案

有两种url。

file:///Users/xxx/Library/Developer/

/Users/xxx/Library/Developer/

当你设置一个用于存储下载文件的url时,你应该使用第一个。当您通过 NSFileManager 从磁盘检索它时,您应该使用第二个。因此进行以下更改。

返回 [NSURL fileURLWithPath:fullPath];

[[NSFileManager defaultManager] attributesOfItemAtPath:[filePath path] error:&fileError];

关于ios - AFNetworking 文件下载错误Error Domain=NSCocoaErrorDomain Code=260,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005768/

相关文章:

ios - 在应用程序扩展和主应用程序中使用 NSUbiquitousKeyValueStore

ios - iOS 15 中的 UIButton 图像行为发生了变化?

php - 如何使用 AFJSONRequestOperation 获取 HTTP 响应

cocoa - 对 NSFileManager 结果进行排序

ios - iPhone 6+ 屏幕尺寸困惑

ios - UICollectionView 与 UIWebView 尺寸问题

ios - 嵌套 json 中的 AFNetworking 帖子图像

Swift 自定义响应序列化程序随机返回图像

objective-c - moveItemAtPath。没有错误,但不工作

objective-c - 在设备上保存一个文件,该文件将在卸载后继续存在