26>ASI文件下载和上传
2015-05-21 23:09
681 查看
1.文件下载,支持断点续传
2.文件的上传
#import "ViewController.h" #import "ASIHTTPRequest.h" @interface ViewController () @property (strong, nonatomic) IBOutlet UIView *progressView; @property (nonatomic,assign) BOOL downloading; @property (nonatomic,strong) ASIHTTPRequest *request; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (self.downloading) { [self.request clearDelegatesAndCancel]; self.downloading = NO; }else{ // 1. 创建url NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/test.mp4"]; // 2. 创建一个请求 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 3. 设置文件的缓存路径 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask , YES) lastObject]; NSString *fileName = [caches stringByAppendingPathComponent:@"test.mp4"]; request.downloadDestinationPath = fileName; // 4. 设置进度监听的代理(想要成为进度监听代理,最好遵守ASIProgressDelegate协议) request.downloadProgressDelegate = self.progressView; // 设置该属性为YES,就会支持断点下载 request.allowResumeForFileDownloads = YES; // 如果要实现断点续传,需要设置一个文件的临时路径 request.temporaryFileDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.temp"]; // 5. 开始请求 [request startAsynchronous]; self.request = request; self.downloading = YES; } } @end
2.文件的上传
#import "HMViewController.h" #import "ASIFormDataRequest.h" #import "ASINetworkQueue.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIProgressView *progressView; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // ASINetworkQueue *queue = [ASINetworkQueue queue]; // queue.shouldCancelAllRequestsOnFailure = YES; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"]; // 2.创建一个请求对象 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; // 3.设置请求参数 [request setPostValue:@"zhangsan" forKey:@"username"]; [request setPostValue:@"123" forKey:@"pwd"]; [request setPostValue:@"28" forKey:@"age"]; [request setPostValue:@"1.89" forKey:@"height"]; // 设置文件参数 NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]; // 如果知道文件路径,最好就用这个方法(因为简单) // ASI内部会自动识别文件的MIMEType [request setFile:file forKey:@"file"]; // [request setFile:file withFileName:@"basic.pptx" andContentType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" forKey:@"file"]; // 如果文件数据是动态产生的,就用这个方法(比如刚拍照完获得的图片数据) // [request setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>]; // 4.设置监听上传进度的代理 request.uploadProgressDelegate = self.progressView; // 5.开始请求 [request startAsynchronous]; // 6.监听完毕 __weak typeof(request) weakRequest = request; [request setCompletionBlock:^{ NSLog(@"%@", [weakRequest responseString]); }]; } @end
相关文章推荐
- 实时开发框架Meteor 实际应用系列<一>---文件的上传和下载
- 文件的上传与下载--->张国亮总结心得第一季
- 文件的上传与下载--->张国亮总结心得第二季
- spring mvc 实现任意文件上传—— 下载<二>
- 实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
- 文件上传与下载—>struts
- python-&gt;FTP上传下载文件
- .NET快速信息化系统开发框架 V3.2->Web版本新增“文件管理中心”集上传、下载、文件共享等一身,非常实用的功能
- 如何编写上传(PC->AS)和下载(AS->PC)程序示例
- 如何编写上传(PC->AS)和下载(AS->PC)程序示例
- 下载centOS-->torrent文件
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- Spring MVC(9):文件上传(单文件,多文件) & 下载
- <input type="file"/> 文件上传
- 利用ASIHttp实现文件的下载、上传和网络状态的监控
- struts2文件下载 <result type="stream">
- <s:file/>多文件上传
- 文件下载:"Content-disposition","attachment; filename=中文名>>>解决方案
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data