-(void)RequestdataUI:(NSString*)ImageURL
imageName:(NSString*)imageName{ NSURL *url = [NSURL URLWithString:ImageURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//创建对象的同时 后台会开启一个新的线程去发出请求了
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *DownloadTask = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *location, NSURLResponse *response,NSError *error) {
// 输出下载文件原来的存放目录
NSLog(@"%@", location); // 设置文件的存放目标路径
NSString *documentsPath = [self getDocumentsPath];
NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL]lastPathComponent]]; // 如果该路径下文件已经存在,就要先将其移除,在移动文件
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[fileURL path] isDirectory:NULL]) {
[fileManager removeItemAtURL:fileURL error:NULL];
}
[fileManager moveItemAtURL:location toURL:fileURL error:NULL]; //--------创建plist文件-----------
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"imagePath.plist"];
NSMutableDictionary *PathPlist= [[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]mutableCopy];
if (PathPlist==nil) {
PathPlist = [[NSMutableDictionary alloc ] init];
}
[PathPlist setObject:imageName forKey:ImageURL];
[PathPlist writeToFile:plistPath atomically:YES ]; }]; [DownloadTask resume]; } - (NSString *)getDocumentsPath {
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = documents[];
NSString* ImagePath =[documentsPath stringByAppendingPathComponent:@"DownloadImage"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//新建文件夹
[fileManager createDirectoryAtPath:ImagePath withIntermediateDirectories:YES attributes:nil error:nil]; return ImagePath; }

NSURLSessionDownloadTask 下载文件的更多相关文章

  1. AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中

    1.下载文件 和进度条处理代码 - (void)timer:(NSTimer *)timer{ // 另一个View中 进度条progress属性赋值 _downloadView.progress = ...

  2. Java下载文件(流的形式)

    @RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...

  3. 使用批处理文件在FTP服务器 上传下载文件

    1.从ftp服务器根目录文件夹下的文件到指定的文件夹下 格式:ftp -s:[配置文件] [ftp地址] 如:ftp -s:c:\vc\ftpconfig.txt   192.168.1.1 建立一个 ...

  4. 通过form表单的形式下载文件。

    在项目中遇到问题,要求动态拼接uri下载文件.但是由于项目的安全拦截导致window.location.href 和 window.open等新建窗口的方法都不行. 无意间百度到了通过form表单来下 ...

  5. SecureCRT上传和下载文件

    SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...

  6. HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    今天,在vs2013中新建了一个placard.json文件,当我用jq读取它的时候,去提示404,直接在浏览器访问这个文件,提示: HTTP 错误 404.3 – Not Found 由于扩展配置问 ...

  7. FTP下载文件失败

    这几天的定时任务下载文件的脚本失败了. 于是手工执行测试,发现报550 Permission denied. Passive mode refused. 意思就是被动模式下,没有权限获取文件. 解决方 ...

  8. 如何使用FileZilla上传和下载文件

    一.使用FileZilla上传文件 1 打开 FileZilla 按照如下图所示,填写远程 Linux 的 IP ,用户名,密码,还有端口号(默认22) 2 选中左边需要上传的文件,然后拖到右边,等待 ...

  9. 开发板tftp下载文件

    搭建过程: 1.安装相关软件包:tftpd(服务端),tftp(客户端),xinetd sudo apt-get install tftpd tftp xinetd 2.建立配置文件(蓝色的目录是可以 ...

随机推荐

  1. java学习之i/o

    基本的输入输出  这里有两种方法,其中一种被注释了,可以换着注释后测试一遍 class TestSystem { public static void main(String[] args) { /* ...

  2. 测试 windows live writer

    This is the first article written by the writer!   wenzhaoshanda

  3. mysql中select into 和sql中的select into 对比

    现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去.answer 01: create table dust select * from student;//用于复制前未 ...

  4. C#如何连接MySql数据库

    最近两天在解决C#连接MySql数据库的问题,通过不同的从网上学习,最终找到了解决的办法,现在和大家分享一下. 1.要连接MySql数据库必须首先下载MySql官方的连接.net的文件,文件下载地址为 ...

  5. node 通过指令创建一个package.json文件及npm安装package.json

    描述包的文件是package.json文件. 一个这样的文件,里面的信息还是挺大的.我们可以放弃手动建立.为了练手我们有命令行来建一个这样的包; 完成name,varsion....license项的 ...

  6. Python Tkinter canvas oval原理

    Ovals, mathematically, are ellipses, including circles as a special case. The ellipse is fit into a ...

  7. {A} + {B}(unique水)

    {A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. git 的一些使用和命令

    http://git.oschina.net/progit/2-Git-%E5%9F%BA%E7%A1%80.html(pro git)   git branch  (展示本地当前的所有分支,以及当前 ...

  9. EPiServer 简单项目总结

    国内用到的EPiServer应该不多,所以记录点用到过的东西,以便分享 1.EPiServer office site http://www.episerver.com/ 2.EPiServer CM ...

  10. Oracle10g安装中遇到的错误及解决办法

    linux解决xhost: unable to open display实用技巧:在Linux下设置xhost方法步骤 第一步:用root登陆linux,启动vnc服务:第二步:根据vnc起来的端口, ...