0

動画ファイルのダウンロードとその進行状況の報告に 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];
}

ここで私が間違っていることを教えてください。どんな助けでも大歓迎です。ありがとう

4

1 に答える 1

2

url には 2 種類あります。

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

/Users/xxx/Library/Developer/.

ダウンロードしたファイルを保存するための URL を設定する場合は、最初の URL を使用する必要があります。によってディスクから取得する場合NSFileManagerは、2 番目のものを使用する必要があります。そのため、次の変更を行います。

return [NSURL fileURLWithPath:fullPath];

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

于 2016-03-15T13:34:08.720 に答える