申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/*
1 导入ASIHTTP类库 配置相关环境 (在本文最后配图) 导入头文件,遵循协议 ASIProgressDelegate
2 自定义一个上传方法
需要一个服务器路径
使用ASI的ASIFormDataRequest 请求
上传的数据(UIImage)
转成Data数据
request 添加data数据
设置委托uploadProgressDelegate
发送异步(或同步)请求
3 自定义一个下载方法
需要一个数据的下载链接url
使用ASI的ASIFormDataRequest 请求
(写一个获取路径的方法)
假如文件太大,先放入临时文件路径 request.temporaryFileDownloadPath
完成文件下载之后 放入文件目标路径 request.downloadDestinationPath
设置委托downloadProgressDelegate
是否允许断点下载allowResumeForFileDownloads
发送异步(或同步)请求
4 获取路径的方法(本次路径设置在沙盒)
用一个path字符串接收沙盒路径
在沙盒路径下,建一个directoryPath目标路径
确保文件夹一定存在 if (![[NSFileManager defaultManager]fileExistsAtPath:directoryPath])增加reateDirectoryAtPath
拼接文件路径和文件名
返回刚刚拼接的路径和路径名
5 假如想看见下载或上传的进度 可以用-(void)setProgress:(float)newProgress
*/
附上ASI库+练习Demo+服务器:http://download.csdn.net/detail/csdn_hhg/9185231
- (void)viewDidLoad {
[super viewDidLoad];
[self upload];
// [self download];
}
// 获取路径
-(NSString *)getFilePathDirectoryName:(NSString *)dName fileName:(NSString *)fName
{
// 用一个path字符串接收沙盒路径
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// 在沙盒路径下,建一个directoryPath目标路径
NSString *directoryPath = [NSString stringWithFormat:@"%@/%@",path,dName];
// 确保文件夹一定 (判断文件夹有没有下载过 没有就创建一个)
if (![[NSFileManager defaultManager]fileExistsAtPath:directoryPath]) {
[[NSFileManager defaultManager]createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
// 拼接文件路径和文件名
NSString *filePath = [NSString stringWithFormat:@"%@/%@",directoryPath,fName];
// 返回刚刚拼接的路径和路径名
return filePath;
}
-(void)download
{
// 模拟数据 随便找的一张图片
NSString *urlString = @"http://e.hiphotos.baidu.com/image/pic/item/eac4b74543a98226f33cdf208882b9014a90eb3c.jpg";
// 使用ASI的ASIFormDataRequest 请求
NSLog(@"路径 %@",NSHomeDirectory());
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//(写一个获取路径的方法)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
// 假如文件比较大 下载的时候先放 临时路径 ~/Documents/TempFiles/downloadFile.jpg
request.temporaryFileDownloadPath = [self getFilePathDirectoryName:@"TempFiles" fileName:@"downloadFile.tmp"];
// 完成文件下载之后 放入文件目标路径 request.downloadDestinationPath
request.downloadDestinationPath = [self getFilePathDirectoryName:@"files" fileName:@"download.jpg"];
// 是否允许断点下载
// 设置委托downloadProgressDelegate
request.allowResumeForFileDownloads = YES;
request.downloadProgressDelegate = self;
// 发送异步(或同步)请求
[request startAsynchronous];
}
-(void)upload
{
// 需要一个服务器路径
NSString *urlString = @"http://127.0.0.1:8080/UploadServer/NewServlet";
// 使用ASI的ASIFormDataRequest 请求
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
// 方法1 上传的数据(UIImage)
UIImage *image = [UIImage imageNamed:@"upload.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1); // 转成Data数据
[request appendPostData:data]; // request 添加data数据
// // 方法2
// [request appendPostDataFromFile: [[NSBundle mainBundle]pathForResource:@"upload" ofType:@"jpg"]];
// 进度代理(遵循协议ASIProgressDelegate 实现协议方法-(void)setProgress:(float)newProgress
// 设置委托uploadProgressDelegate
request.uploadProgressDelegate = self;
// 发送异步(或同步)请求
[request startAsynchronous];
}
// 假如想看见下载或上传的进度 可以用
-(void)setProgress:(float)newProgress
{
NSLog(@"进度 %F",newProgress);
}