1.上传图片以二进制流的形式上传
1 #pragma mark - 文件上传
2 - (IBAction)uploadImage
3 {
4 /*
5 此段代码如果需要修改,可以调整的位置
6
7 1. 把upload.php改成网站开发人员告知的地址
8 2. 把file改成网站开发人员告知的字段名
9 */
10 // 1. httpClient->url
11
12 // 2. 上传请求POST
13 NSURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
14 // 在此位置生成一个要上传的数据体
15 // form对应的是html文件中的表单
16
17
18 UIImage *image = [UIImage imageNamed:@"头像1"];
19 NSData *data = UIImagePNGRepresentation(image);
20
21 // 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
22 // 要解决此问题,
23 // 可以在上传时使用当前的系统事件作为文件名
24 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
25 // 设置时间格式
26 formatter.dateFormat = @"yyyyMMddHHmmss";
27 NSString *str = [formatter stringFromDate:[NSDate date]];
28 NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
29
30
31 /*
32 此方法参数
33 1. 要上传的[二进制数据]
34 2. 对应网站上[upload.php中]处理文件的[字段"file"]
35 3. 要保存在服务器上的[文件名]
36 4. 上传文件的[mimeType]
37 */
38 [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];
39 }];//file改为后台接收的字段或参数
40
41 // 3. operation包装的urlconnetion
42 AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
43
44 [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
45 NSLog(@"上传完成");
46 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
47 NSLog(@"上传失败->%@", error);
48 }];
49
50 //执行
51 [_httpClient.operationQueue addOperation:op];
当要上传多张图片时只需在multipartFormRequestWithMethod方法上添加这些代码就好
AFNetWorking使用multipartFormRequestWithMethod方法上传多张图片问题
int i=0;
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
for(UIImage *eachImage in array)
{
NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"abc%d.jpg",i ] mimeType:@"image/jpeg"];//file改为后台接收的字段或参数
i++;
}
}];
2.上传图片以二进制流的字符串的形式上传
-(void)postPhotosToShare_API23_withPid:(NSString *)_pid andUid:(NSString *)_uid andScore:(float)_score andContent:(NSString *)_content andAnonymous:(NSString *)_anonymous andImgArray:(NSMutableArray *)_imgArray
{
path = @"interface/product.php/product/";//path为网站开发人员告知的除去IP后的地址
NSURL *baseUrl1 = [NSURL URLWithString:urlIP];//urlIP为网站开发人员告知的IP地址,例:http://192..168.1.1
httpClient = [[AFHTTPClient alloc]initWithBaseURL:baseUrl1];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
[parameters setObject:_pid forKey:@"pid"];
[parameters setObject:_uid forKey:@"uid"];
[parameters setObject:[NSString stringWithFormat:@"%f",_score] forKey:@"score"];
[parameters setObject:_content forKey:@"content"];
[parameters setObject:_anonymous forKey:@"anonymous"];
if (_imgArray.count!=0)
{
int imgCount=0;
for (UIImage *myImg in _imgArray)
{
NSData *imageData = UIImageJPEGRepresentation(myImg,0.7);//进行图片压缩
NSString *_encodedImageStr = [imageData base64Encoding];//进行64位转码转为字符串
[parameters setObject:_encodedImageStr forKey:[NSString stringWithFormat:@"img[%i]",imgCount]];//进行img[%i]改为后台接收的字段或参数
imgCount ++;
}
}
request = [httpClient requestWithMethod:@"POST" path:path parameters:parameters];
[request setTimeoutInterval:kDataExpiryTime];//设置请求时间
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
[self getResultSuccess:json withTage:Get_API_Tag_23];//对api进行标记,可要可不要
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self getResultFailed:error];
}];
[operation start];
}
iOS利用AFNetworking(AFN) 实现图片上传