关于AFUrlSessionManager下载文件报The file couldn’t be opened because the specified URL type isn’t support问题

来源:互联网 发布:网络事件 2017 编辑:程序博客网 时间:2024/06/05 16:21

今天遇到一个问题,就是用AFUrlSessionManager下载文件的时候,下载过程中一切正常,但是去解压这个文件的时候,却发现该文件不存在,找了半天还以为是路径有问题,确定了很多遍发现路径没有问题,最后一点点跟代码才发现问题原因,这里记录一下。

AFUrlSessionManager下载文件的流程是先将文件下载到一个临时文件夹下面,然后移动到指定的目录下面并重新命名,问题就是发生在这里,打断点发现是移动.tmp文件的时候出错了,移动文件的代码如下:

if (self.downloadTaskDidFinishDownloading) {        self.downloadFileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location);        if (self.downloadFileURL) {            [[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError];            NSLog(@"fileManagerError=%@",fileManagerError);            if (fileManagerError) {                [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];            }        }    }

错误信息如下:

The file couldn’t be opened because the specified URL type isn’t supported.

最后发现,这个NSFileManager读取本地文件的时候,必须加上file://这个url头,不然就会报这个错,我这里的两个参数location和self.downloadFileURL前者有加这个头,后者是自己定义的沙盒路径没有加这个,所以移动文件的时候会报错,使用[NSURL fileURLWithPath:filePath]返回的就是加上了file://头的本地路径了。

另外需要注意的是,如果目标路径下存在了名称一样的文件,是移不过去的,需要先将同名文件删除,再移动。

阅读全文
0 0
原创粉丝点击