-(void)upload{ AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{@"deviceName":@"imageName",@"deviceId":@"1",@"deviceChildId":@"11"}; [manager POST:@"http://127.0.0.1:8080/upload" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { UIImage *image = [UIImage imageNamed:@"a.jpg"]; NSData *imageData = UIImageJPEGRepresentation(image, 1); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str]; // 上传图片,以文件流的格式 [formData appendPartWithFileData:imageData name:@"deviceImg" fileName:fileName mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"上传成功返回结果 : %@",responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error::::%@",error); }]; }
时间: 2024-10-11 04:11:34