NSURLSessionDownloadTask:如何更改临时文件的保存位置

问题描述 投票:1回答:1

在正常情况下,下载(视频)文件将保存在位置路径(.tmp)下,然后使用以下de; egate方法将文件(.tmp)移动到目标文件夹。

但是我想进行下载和播放,如何在下载之前将文件路径(位置)更改为目标路径(目标URL)。

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *destinationFileName = downloadTask.originalRequest.URL.lastPathComponent;
    NSURL *destinationURL = [self.downloadDirURL URLByAppendingPathComponent:destinationFileName];
    if([fileManager fileExistsAtPath:[destinationURL path]])
    {
        [fileManager removeItemAtURL:destinationURL error:nil];
    }
    BOOL success = [fileManager moveItemAtURL:location toURL:destinationURL error:&error];
}
ios preload
1个回答
0
投票

但是我想进行下载和播放,如何在下载之前将文件路径(位置)更改为目标路径(目标URL)

你不能。您正在做的事情对于下载任务是正确的:下载到它下载的位置(这与您的无关),并在下载完成后立即将其移动到有用的位置。

(但请注意,您不需要下载视频文件只是为了播放它。您可以开始在Internet上播放文件。所以这里的问题可能是您首先要下载的。)

© www.soinside.com 2019 - 2024. All rights reserved.