0

我正在使用AFNetworking 3.0.4進行視頻文件下載及其進度報告。該文件似乎下載成功,但當我保存此文件,並嘗試獲取文件屬性,然後它給了我以下錯誤。AFNetworking文件下載錯誤Error Domain = NSCocoaErrorDomain代碼= 260

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字典是零,它給了我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]; 
} 

一些機構可以請指導我我在做什麼錯在這裏。任何幫助將不勝感激。謝謝

回答

1

有兩種類型的網址。

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

/Users/xxx/Library/Developer/

當您設置存儲下載文件的url時,應該使用第一個。當您通過NSFileManager從磁盤檢索它時,應該使用第二個。所以做出以下更改。

return [NSURL fileURLWithPath:fullPath];

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