NSURLSessionDownloadTask 下载文件
-(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 下载文件的更多相关文章
- AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中
1.下载文件 和进度条处理代码 - (void)timer:(NSTimer *)timer{ // 另一个View中 进度条progress属性赋值 _downloadView.progress = ...
- Java下载文件(流的形式)
@RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...
- 使用批处理文件在FTP服务器 上传下载文件
1.从ftp服务器根目录文件夹下的文件到指定的文件夹下 格式:ftp -s:[配置文件] [ftp地址] 如:ftp -s:c:\vc\ftpconfig.txt 192.168.1.1 建立一个 ...
- 通过form表单的形式下载文件。
在项目中遇到问题,要求动态拼接uri下载文件.但是由于项目的安全拦截导致window.location.href 和 window.open等新建窗口的方法都不行. 无意间百度到了通过form表单来下 ...
- SecureCRT上传和下载文件
SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...
- HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
今天,在vs2013中新建了一个placard.json文件,当我用jq读取它的时候,去提示404,直接在浏览器访问这个文件,提示: HTTP 错误 404.3 – Not Found 由于扩展配置问 ...
- FTP下载文件失败
这几天的定时任务下载文件的脚本失败了. 于是手工执行测试,发现报550 Permission denied. Passive mode refused. 意思就是被动模式下,没有权限获取文件. 解决方 ...
- 如何使用FileZilla上传和下载文件
一.使用FileZilla上传文件 1 打开 FileZilla 按照如下图所示,填写远程 Linux 的 IP ,用户名,密码,还有端口号(默认22) 2 选中左边需要上传的文件,然后拖到右边,等待 ...
- 开发板tftp下载文件
搭建过程: 1.安装相关软件包:tftpd(服务端),tftp(客户端),xinetd sudo apt-get install tftpd tftp xinetd 2.建立配置文件(蓝色的目录是可以 ...
随机推荐
- php安装扩展模块(curl模块)
php安装扩展模块的思路: 1,首先找到需要安装的扩展模块的目录.一般在/usr/local/php/ext目录下 但是有的模块php源码中不一定有,需要自己下载比如memcache.redis等. ...
- Android项目能运行,上传svn后再下载却不能运行
今天遇到一个比较奇怪的问题,android项目上传到svn之前,可以运行,但是上传到svn后再check, 就出错. 搜索了一下,发现真的解决了问题. svn 不知道是出于什么原因,不能上传.so文件 ...
- Letter of application, e-mail version
Subject line: (logical to recipient!) Application for sales representative for mid-Atlantic area Apr ...
- sql server数据同步方案-日志传送
1 功能描述 本方案采用日志传送模式,把核心数据库(主数据库)定期同步到灾备数据库(辅助服务器)及备份库(辅助服务器,便于其他系统使用,减轻主数据压力),期间,如果发生异常导致无法同步,将以电子邮件. ...
- 整理js继承
实现继承的方法: 一,原型链:利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType(){ this.property=true; } SuperType.prot ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- 如何让secureCRT显示Linux的颜色
style="padding-bottom: 0px; line-height: 1.5; margin: 0px; padding-left: 0px; padding-right: 0p ...
- primary key与unique的区别
定义了 UNIQUE 约束的字段中不能包含重复值,可以为一个或多个字段定义 UNIQUE 约束.因此,UNIQUE 即可以在字段级也可以在表级定义, 在UNIQUED 约束的字段上可以包含空值.ORA ...
- python操作 redis-list
#!/usr/bin/python #!coding: utf-8 import redis if __name__=="__main__": try: conn=redis.St ...
- SQL Server 索引整理与堆重组。
重新组织索引: alter index idx_OrderID on dbo.OrderDetail reorganize | reorganize;---可以rebuild 也可 ...