首页 > 代码库 > ASIFormDataRequest 上传图片
ASIFormDataRequest 上传图片
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self request];}- (void)saveImage{ NSData *imageData = http://www.mamicode.com/UIImageJPEGRepresentation(image, 1.0); [imageData writeToFile:[self GetTempPath] atomically:YES];}-(NSString *)GetTempPath{ NSString *tempPath =[NSString stringWithFormat:@"%@/upload.jpg",NSTemporaryDirectory()]; return tempPath;}- (void)request{ NSDictionary *postDic = [[NSDictionary alloc]init]; [postDic setValue:@"test" forKey:@"userId"]; [postDic setValue:[self GetTempPath] forKey:@"file"]; [self uploadImage:postDic];}- (void)uploadImage:(NSDictionary *)dic{ ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:postUrl]]; // 通常数据是以’application/x-www-form-urlencoded’格式发送的,如果上传了二进制数据或者文件,那么格式将自动变为‘multipart/form-data’注意要和服务器统一。 [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"]; //超时时间 request.timeOutSeconds = 30; //定义异步方法 [request setDelegate:self]; [request setRequestMethod:@"POST"]; [request setDidFailSelector:@selector(commonRequestDidFailed:)]; [request setDidFinishSelector:@selector(commonRequestDidSuccess:)]; //post的数据 id key, value; for (int i = 0; i < [dic allKeys].count; i++) { key = [keys objectAtIndex: i]; value = [params objectForKey: key]; NSLog (@"Key: %@ for value: %@", key, value); if ([key isEqualToString:@"file"]) { [request setFile:value forKey:key]; [request setShouldStreamPostDataFromDisk:YES]; } else { [request setPostValue:value forKey:key]; } } [request startSynchronous];}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。