多圖上傳
NSString *mnPath = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
NSString *cyPath = [[NSBundle mainBundle] pathForResource:@"草原.jpg" ofType:nil];
UIImage *mnImage = [[UIImage alloc] initWithContentsOfFile:mnPath];
UIImage *cyImage = [[UIImage alloc] initWithContentsOfFile:cyPath];
NSData *mnData = UIImageJPEGRepresentation(mnImage, 0.7f);
NSData *cyData = UIImageJPEGRepresentation(cyImage, 0.7f);
NSArray *dataArray = @[mnData,cyData];
//發(fā)起網(wǎng)絡(luò)請(qǐng)求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// 上傳多張圖片
for(NSInteger i = 0; i < dataArray.count; i++)
{
// 取出單張圖片二進(jìn)制數(shù)據(jù)
NSData * imageData = dataArray[i];
// 圖片數(shù)據(jù)對(duì)應(yīng)的key值,沒(méi)有這個(gè)key 服務(wù)器$_FILES 獲取不到數(shù)據(jù)流
NSString * Name = [NSString stringWithFormat:@"%ld", i+1];
// 圖片名
NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
[formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
單圖上傳
NSString *path = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:path];
NSData *data = UIImageJPEGRepresentation(savedImage, 0.7f);
// 發(fā)起網(wǎng)絡(luò)請(qǐng)求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// name :圖片數(shù)據(jù)對(duì)應(yīng)的key值,沒(méi)有這個(gè)key 服務(wù)器$_FILES 獲取不到數(shù)據(jù)流
[formData appendPartWithFileData:data name:@"file" fileName:@"WE.jpg" mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
服務(wù)端
/**
* 多圖/單圖上傳
*/
public function upload_goods(){
$upload = new \Think\Upload();// 實(shí)例化上傳類
$upload->maxSize = 4000000 ;// 設(shè)置附件上傳大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型
$upload->rootPath = './uploads/goods_img/'; // 設(shè)置附件上傳根目錄
$upload->savePath = ''; // 設(shè)置附件上傳(子)目錄
$upload->autoSub = true;
// 上傳文件
$info = $upload->upload($_FILES);
if(!$info) {
$this->ajaxReturn(apiData($upload->getError(), true));
}else{
$this->ajaxReturn(apiData($info, true));
}
}
返回?cái)?shù)據(jù)

backData.png