根据url地址下载文件到本地,返回本地文件地址
【摘要】
-(NSString*)DownloadTextFile:(NSString*)fileUrl fileName:(NSString*)_fileName{ NSString *sandboxPath = NSHomeDirectory(); NSString *documentPath = [sandboxPath ...
-
-(NSString*)DownloadTextFile:(NSString*)fileUrl fileName:(NSString*)_fileName
-
{
-
NSString *sandboxPath = NSHomeDirectory();
-
NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];//将Documents添加到sandbox路径上
-
NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名
-
NSFileManager *fileManager = [NSFileManager defaultManager];
-
-
if ([fileManager fileExistsAtPath:FileName])
-
{
-
return FileName;
-
}else
-
{
-
NSURL *url = [NSURL URLWithString:fileUrl];
-
NSLog(fileUrl);
-
NSData *data = [NSData dataWithContentsOfURL:url];
-
[data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName
-
}
-
return FileName;
-
}
/打开本地文件,获取文件路径
-
NSString *FileName = @"myWebURL.plist";
-
NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-
NSString *cacheDir = [cachePath objectAtIndex:0];
-
NSString *FilePath = [cacheDir stringByAppendingPathComponent:FileName];
-
-
-
NSFileManager *fileManager = [NSFileManager defaultManager];
-
// Copy the file from the resourcepath to the documentpath
-
if (![fileManager fileExistsAtPath:FilePath])
-
{
-
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FileName];
-
NSError *error;
-
[fileManager copyItemAtPath:filePathFromApp toPath:FilePath error:&error];
-
}
-
NSLog(FilePath);
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/8844413
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)