下面的截圖,就是Img以file文件類型上傳:
1.AFNetworking最新犁珠,3.0中也有封裝好的方法逻炊,不過暫時沒弄出來,項(xiàng)目做完在總結(jié)犁享。
2.就是用系統(tǒng)自帶就行處理.
下面是封裝好的余素,絕對可用,只需要改變參數(shù)就可以了
下面加粗,斜線的地方炊昆,就是你參數(shù)名桨吊,需要修改的地方
+ (void)uploadImg:(UIImage *)image
urlString:(NSString *)urlString
parameters:(NSDictionary *)parameters
callBack:(RequestCallback)callBack {
//分界線的標(biāo)識符
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//根據(jù)url初始化request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
//分界線 --AaB03x
NSString *MPboundary=[[NSString alloc] initWithFormat:@"--%@", TWITTERFON_FORM_BOUNDARY];
//結(jié)束符 AaB03x--
NSString *endMPboundary=[[NSString alloc] initWithFormat:@"%@--", MPboundary];
//得到圖片的data
// 壓縮圖片
NSData *imageData = nil;
CGFloat compression = 1.0;
do {
imageData = UIImageJPEGRepresentation(image, compression);
compression -= 0.1;
} while (imageData.length / 1024.0 > 300);
//http body的字符串
NSMutableString *body = [[NSMutableString alloc] init];
//參數(shù)的集合的所有key的集合
for (NSString *key in [parameters allKeys]) {
//添加分界線,換行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名稱凤巨,換2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[parameters objectForKey:key]];
}
////添加分界線视乐,換行
[body appendFormat:@"%@\r\n",MPboundary];
//聲明pic字段,文件名為boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"headPortrait\"; filename=\"headPortrait.png\"\r\n"];
//聲明上傳文件的格式
[body appendFormat:@"Content-Type: image/png\r\n\r\n"];
//聲明結(jié)束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
//聲明myRequestData敢茁,用來放入http body
NSMutableData *myRequestData=[NSMutableData data];
//將body字符串轉(zhuǎn)化為UTF8格式的二進(jìn)制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//將image的data加入
[myRequestData appendData:imageData];
//加入結(jié)束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//設(shè)置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//設(shè)置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//設(shè)置Content-Length
[request setValue:[NSString stringWithFormat:@"%ld", (long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//設(shè)置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
dispatch_sync(dispatch_get_main_queue(), ^{
// 請求失敗佑淀,返回錯誤對象
if (connectionError) {
if (callBack) {
callBack(RequestFailed, connectionError);
} else {
NSLog(@"Request failed with reason '%@'", [connectionError localizedDescription]);
}
return;
}
id object = [TYTools JSONObjectWithData:data];
// 請求成功,解析成功彰檬,返回json對象
if (object) {
if (callBack) {
callBack(RequestSuccess, object);
}
}
// 請求成功伸刃,解析失敗,返回反饋?zhàn)址?/p>
else {
if (callBack) {
callBack(RequestException, object);
NSLog(@"數(shù)據(jù)異常:%@", object);
}
}
});
}];
}