ios 上传file类型图片

2017-12-08  本文已影响16人  橘子味草莓

图片一定要先压缩,转成data,不然就会报错

AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];

manager.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",

@"text/html",

@"image/jpeg",

@"image/png",

@"application/octet-stream",

@"text/json",

@"image/jpg",

@"text/plain",

nil];

[managerPOST:[NSStringstringWithFormat:@"%@%@",POSTURL,WXHOMESENDDYNAMIC]parameters:dictconstructingBodyWithBlock:^(id_NonnullformData) {

// 上传多张图片

for(NSIntegeri =0; i

{

//取出单张图片二进制数据

NSData*imageData =UIImageJPEGRepresentation(self.imageArr[i],0.8);

NSLog(@"%@",imageData);

// 上传的参数名,在服务器端保存文件的文件夹名

NSString* Name = [NSStringstringWithFormat:@"file"];

// 上传filename

NSString* fileName = [NSStringstringWithFormat:@"%@.jpg", Name];

[formDataappendPartWithFileData:imageDataname:NamefileName:fileNamemimeType:@"image/jpeg"];

}

}progress:^(NSProgress*_NonnulluploadProgress) {

NSLog(@"%@",uploadProgress);

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

if([responseObject[@"code"]integerValue]==10000) {

[selfshowText:@"上传成功"];

[self.navigationControllerpopViewControllerAnimated:YES];

}else{

[selfshowText:[NSStringstringWithFormat:@"%@",responseObject[@"message"]]];

NSLog(@"%@",responseObject);

}

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

[selfshowError:error];

}];

上一篇 下一篇

猜你喜欢

热点阅读