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.建立配置文件(蓝色的目录是可以 ...
随机推荐
- RMAN完整全备份
1.以CATALOG模式连接到目标数据库和恢复目录(如:目标数据库为ORCL) C:\Users\Administrator>RMAN TARGET / CATALOG RMANCT/RMANC ...
- 【贪心】【uva11520】 Fill the Square
填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...
- openssl 安装
六.运行“nmake -f ms\ntdll.mak install”安装编译后的OpenSSL到指定目录. 七.查看安装结果C:\usr\local\ssl或C:\openssl-0.9.8.e下包 ...
- android 补间动画
android开发过程中,为了更好的展示应用程序,应用程序添加动画,能够很好地实现这个功能.如果动画中的图像变化有一定的规律,可以采用自动生成图像的方式来生成动画,例如图像的移动.旋转.缩放等.自动生 ...
- Jq合成事件绑定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iOS之短信认证
短信验证 现在很多的短信验证平台,我们比较常用的有移动开发者服务平台 根据短信验证文档来集成 1. 找到iOS短信验证的集成开发文档 2. 下载SDK和Demo目录结构 3. 运行Demo 4. 写 ...
- 客户端js判断文件类型和文件大小即限制上传大小
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- sql 事务和回滚
(1) set ANSI_NULLS ON --见图1 set QUOTED_IDENTIFIER ON go ALTER proc [dbo].[procname] as begin begin t ...
- c# ListView
// Attempt to run the file. System.Diagnostics.Process.Start(filename); //folderCol 可以存放一个路径的 栈(用于返回 ...
- Java转换
1.如何将字符串String转化为整数int int i = Integer.parseInt(str); int i = Integer.valueOf(my_str).intValue(); ...