我想通过POST方法上传图像到服务器。 格式应该像["ImageName"=Image]
。 我曾尝试下面的代码,但它不将图像上传到服务器。 谁能帮我 ?
-(void)SaveImageToServer:(UIImage*)getImage :(NSString*)imageName :(NSString*)getUrlToSaveImg withCompletionBlock:(void(^)(NSMutableArray *))completionBlock
{
NSMutableArray *arrrrr=[[NSMutableArray alloc]init];
NSMutableURLRequest *reqqq=[[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:getUrlToSaveImg]];
NSData *dataOfImg=UIImagePNGRepresentation(getImage);
NSString *stringImage = [dataOfImg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *concat=[NSString stringWithFormat:@"fileName=%@",stringImage];
NSData *dataaa = [concat dataUsingEncoding:NSUTF8StringEncoding];
[reqqq setHTTPMethod:@"POST"];
[reqqq setHTTPBody:dataaa];
NSURLSessionConfiguration *configg=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession*sessionn=[NSURLSession sessionWithConfiguration:configg delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *taskk=[sessionn dataTaskWithRequest:reqqq completionHandler:^(NSData *data,NSURLResponse *responce,NSError *error){
if(error)
{
NSLog(@"%@", [error localizedDescription]);
completionBlock(nil);
}else{
NSMutableDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];
NSLog(@"data %@",d);
if (d) {
[arrrrr addObject:d];
}
if (completionBlock) {
completionBlock(arrrrr);
}
}
}];
[task resume];
}