我想将我的图像从手机图库上载到服务器,所以我无法上载程序,程序运行成功并显示打印-
NSLog(@">>>>>>>>>> enter in ");
但是它无法在服务器上上传图像,当我在应用程序中检查图像时,没有图像,并且我还检查了参数,我认为我没有在参数中发送正确的文件格式。 请任何人帮助我提供正确的文件格式如何将其转换
`
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)img
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
NSURL *imagePath = [editingInfo objectForKey:@"UIImagePickerControllerReferenceURL"];
imageName = [imagePath lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
localFilePath = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(@"localFilePath.%@",localFilePath);
}
- (IBAction)submitBtn:(id)sender
{
NSURL* url;
url = [NSURL URLWithString:UrlBasic];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
manager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
fileURL = [NSURL fileURLWithPath:localFilePath];
reqData=[[NSMutableDictionary alloc]initWithObjectsAndKeys:imageName1,@"image",@"addclassphotoactmobs",@"droot",schoolFolderA,@"schoolfolder",_choseGalleryTextF.text,@"gname",dividNum,@"classid",fileURL,@"uploadedfile",nil];
NSLog(@"reqData=%@",reqData);
[manager POST:UrlBasic parameters:reqData constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { }
progress:nil
success:^(NSURLSessionTask *task, NSMutableDictionary *responseObject) {
NSLog(@" %@",responseObject);
NSLog(@">>>>>>>>>> enter in ");
[self.view makeToast:@"submitted ....."
duration:3.0
position:CSToastPositionCenter];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error-=%@",error);
// [self.view makeToast:@"Please check internet connection !"];
}];
}
答案 0 :(得分:0)
[使用imageNameStr更新]
上传图像数据时,必须发送文件名。要生成文件名,这里我使用了日期和时间。
在NSLog(@"reqData=%@",reqData);
行下添加代码
===================
您错过了formData代码。 在下面的代码中,图像是实际图像。
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyyMMddhhmmssSSS"];
NSString *imageNameStr = [NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:[NSDate date]]];
[sessionManager POST:appendURL
parameters:postParamDict
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if(image!=nil){
NSData * imageData = UIImageJPEGRepresentation(image,0.5f);
if(imageData!=nil){
[formData appendPartWithFileData:imageData
name:@"image"
fileName:imageNameStr
mimeType:@"image/jpg"];
}
}
}
progress:^(NSProgress * _Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"%@",responseObject);
if(success)
success (responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error %@",error);
if(failure)
failure (error);
}]