利用 ASIFormDataRequest 上传文件
1。首先,要下载 ASIHTTPRequest 框架,也就是一堆文件。
然后导入到 工程中。
2。添加必要的框架到项目中去
CFNetwork.framework
SystemConfiguration.framework
libz.1.2.3.dylib
SystemConfiguration.framework + zlib
CoreService.framework
3。代码:
#import "ASIFormDataRequest.h"
#define HEADER_URL_POST_IMAGE @"http://uppic.xxyyy.com/ports/face/upload?a=%@&t=%@&f=iphone" std::string username = @"sss"; std::string token_signature = @"bbb"; NSString* node = [NSString stringWithCString:username.c_str() encoding:NSUTF8StringEncoding]; NSString* token = [NSString stringWithCString:token_signature.c_str() encoding:NSUTF8StringEncoding]; NSString* token_url_encoded = URLENCODING(token); //拼接URL字符串 NSString* baseUrl = [[[NSString alloc] init] autorelease]; baseUrl = [NSString stringWithFormat:HEADER_URL_POST_IMAGE, node, token_url_encoded]; //创建URL 请求 NSURL *url = [NSURL URLWithString:baseUrl]; //发送上传请求 ASIFormDataRequest * formRequset=[ASIFormDataRequest requestWithURL:url]; [formRequset setFile:[NSString stringWithCString:avatarPath.c_str() encoding:NSUTF8StringEncoding] forKey:@"face_file"]; formRequset.delegate=self; [formRequset startSynchronous]; //提示是否上传成功 if ([formRequset complete]) { UIAlertView *msgView=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"上传新头像成功" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [msgView autorelease]; [msgView show]; } else { UIAlertView *msgView=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"上传新头像失败,请稍后再试" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [msgView autorelease]; [msgView show]; }