现在的位置: 首页 > 综合 > 正文

IOS下载文件

2013年02月27日 ⁄ 综合 ⁄ 共 896字 ⁄ 字号 评论关闭
-(NSString*)DownloadTextFile:(NSString*)fileUrl   fileName:(NSString*)_fileName
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函数NSSearchPathForDirectoriesInDomains来获得沙盒中目录的全路径。
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    NSString *sandboxPath = NSHomeDirectory();
    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"Documents"];//将Documents添加到sandbox路径上
    NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Copy the database sql file from the resourcepath to the documentpath
    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;
}

抱歉!评论已关闭.