13

我有一个应用程序使用backgroundSessionConfiguration实例NSURLSession来处理一些NSURLSessionDownloadTask任务。任务已正确创建并完成下载,但是URLSession:downloadTask:didFinishDownloadingToURL:当我将下载的文件从location磁盘上的永久位置移动时,有时(阅读:经常)会收到错误:

NSUnderlyingError=0x178247890 "The operation couldn’t be completed. No such file or directory"

就好像在我有机会移动它之前,下载的文件已从临时目录 (../Library/Caches/com.apple.nsnetworkd/..) 中清除。

对同一个远程文件运行操作将在所有其他因素相同的情况下以两种方式运行,因此我无法确定任何会导致它有时工作的东西。

无论如何,有没有办法确保这个文件在完成后会保留足够长的时间以便将其移动到位?

编辑:对于正常(非背景)会话,我也经常看到这种情况

4

4 回答 4

10

退出此方法后临时文件会自动删除,您可以尝试将这个返回的文件保存为一个NSData并直接保存到所需的位置。

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {

    // Create a NSFileManager instance
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    // Get the documents directory URL
    NSArray *documentURLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [documentURLs firstObject];

    // Get the file name and create a destination URL
    NSString *sendingFileName = [downloadTask.originalRequest.URL lastPathComponent];
    NSURL *destinationUrl = [documentsDirectory URLByAppendingPathComponent:sendingFileName];

    // Hold this file as an NSData and write it to the new location
    NSData *fileData = [NSData dataWithContentsOfURL:location];
    [fileData writeToURL:destinationUrl atomically:NO];
}
于 2014-05-16T12:48:48.087 回答
3

尝试这个

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [URLs objectAtIndex:0];
    NSURL *originalURL = [[downloadTask originalRequest] URL];
    if(originalURL)
       destinationUrl = [documentsDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];

    NSError *errorCopy;

    [fileManager removeItemAtURL:destinationUrl error:NULL];
    BOOL success = [fileManager copyItemAtURL:downloadURL toURL:destinationUrl error:&errorCopy];

    if (success) {

    } else {

    }
}
于 2014-05-22T06:55:07.390 回答
0

Ran into this using Swift under iOS 8, copy or move file did not work. Reading temp file directly and writing back out worked. Code creates image view from downloaded image:

var fileHandle:NSFileHandle = NSFileHandle(forReadingAtPath: location.path!)!
var data:NSData = fileHandle.readDataToEndOfFile()
var image:UIImage = UIImage(data: data)!
imageView.image = image
于 2014-11-03T20:35:41.603 回答
0

这是最好的方法

- (void)URLSession:(NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location{

    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray      *documentURLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL  *documentsDirectory = [documentURLs firstObject];

    // Get the file name and create a destination URL
    NSString *sendingFileName = [downloadTask.response suggestedFilename];
    NSURL     *destinationUrl = [documentsDirectory URLByAppendingPathComponent:sendingFileName];

    // Hold this file as an NSData and write it to the new location
    NSData *fileData = [NSData dataWithContentsOfURL:location];
    [fileData writeToURL:destinationUrl atomically:NO];
}
于 2016-04-13T18:29:35.640 回答