I am having an app in which I am uploading user's data on webserver.
I am using the below code to pass the data to webserver with the code below.
-(IBAction)btnRegister:(id)sender
{
jsonSP = [SBJSON new];
jsonSP.humanReadable = YES;
NSString *service = @"/register.php";
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"register_btn.png"]);
[Base64 initialize];
NSString *imageString = [Base64 encode:imageData];
NSLog(@"%@",imageString);
NSString *requestString = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&\"user_password\"=\"%@\"&\"first_name\"=\"%@\"&\"email\"=\"%@\"&\"profile_photo_link\"=\"%@\"\"}",@"Test",@"Test",@"Test",@"Test@mmm.com",imageString];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc = [urlLoc stringByAppendingString:service];
NSLog(@"URL : %@",urlLoc);
// urlLoc = [urlLoc stringByAppendingString:@"?username=abcd&password=abcd"];
urlLoc = [urlLoc stringByAppendingString:requestString];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"{" withString:@""];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"}" withString:@""];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"URL : %@",urlLoc);
// [fileContents release]; //o_r
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
// self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
NSLog(@"response is %@",returnData);
// [request release];
if (respError)
{
//[customSpinner hide:YES];
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Internet connection is not Available!" message:@"Check your network connectivity" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alt show];
//[alt release];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"response string %@",responseString);
}
}
I am successfully able to send the data to webserver without the image.
But if upload the image with the code shown above by encoding Base64 then the data is not uploaded on webserver. It gives me an error in NSLog saying request query string too long.
I have already imported Base64.h and .m files.
I want to pass the data with image also.
How can I do that?
I know there are lots of links but I want to pass it this way so please tell me what is wrong I am doing here?
Please help me.
Thanks in advance.
Hey Manthan Try this bellow code for convert UIImage
to NSString
and then pass it to the server
UIImage *imgPic =[UIImage imageNamed:@"register_btn.png"];
NSData * imageData = UIImagePNGRepresentation(imgPic);
NSString *base64String = [imageData base64EncodedStringWithOptions:0];
its a server problem, and its telling you with that error.
There is not a maximum length for a json, but there is a maximum amount of data you can send. For fix this, just set the amount of data you can send in the .htaccess file: php_value post_max_size 20M
EDIT
Also, if you have access to the php.ini you can change it in post_max_size
row
Try like this ;
- (void)addNewItem:(SH_NewItem *)newItem withCompletitionBlock:(void(^)(NSArray *items, NSError *error, NSDictionary *userInfo))completitionBlock
{
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[[SH_Shared sharedInstance] getBaseURL]]];
//main cover image...
NSData *imageData = UIImageJPEGRepresentation(newItem.image, JPEG_IMAGE_QUALITY);
NSData *imageData1 = UIImageJPEGRepresentation(newItem.image1, JPEG_IMAGE_QUALITY);
NSData *imageData2 = UIImageJPEGRepresentation(newItem.image2, JPEG_IMAGE_QUALITY);
NSData *imageData3 = UIImageJPEGRepresentation(newItem.image3, JPEG_IMAGE_QUALITY);
NSData *imageData4 = UIImageJPEGRepresentation(newItem.image4, JPEG_IMAGE_QUALITY);
NSDictionary *params = [[NSDictionary alloc]init];
params = [NSDictionary dictionaryWithObjectsAndKeys:
newItem.title, @"title",
newItem.itemDescription, @"description",
[[SH_Shared sharedInstance] token], @"token",
nil];
NSLog(@"params-%@",params);
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"new_item" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
if (imageData != nil)
[formData appendPartWithFileData:imageData name:@"images[0]" fileName:@"image_ad.png" mimeType:@"multipart/form-data"];
if (imageData1 != nil)
[formData appendPartWithFileData:imageData1 name:@"images[1]" fileName:@"image_ad1.png" mimeType:@"multipart/form-data"];
if (imageData2 != nil)
[formData appendPartWithFileData:imageData2 name:@"images[2]" fileName:@"image_ad2.png" mimeType:@"multipart/form-data"];
if (imageData3 != nil)
[formData appendPartWithFileData:imageData3 name:@"images[3]" fileName:@"image_ad3.png" mimeType:@"multipart/form-data"];
if (imageData4 != nil)
[formData appendPartWithFileData:imageData4 name:@"images[4]" fileName:@"image_ad4.png" mimeType:@"multipart/form-data"];
}];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[self getDataWithOperation:op completitionBlock:^(NSData *data, NSError *error, NSDictionary *userInfo) {
CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
NSError *deserializeError = nil;
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", str);
NSDictionary *requestResponseDict = [deserializer deserialize:data error:&deserializeError];
NSString *status = [requestResponseDict valueForKey:kStatus];
BOOL isSuccess = [[status valueForKey:kSuccess] boolValue];
if (isSuccess)
{
// it's OK
}
else
{
// its error
}
}];
}
I did it working with the following code with multipart form data.
responseData = [NSMutableData data];
NSString *service = @"http:///registerUser.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:service]];
[request setHTTPMethod:@"POST"];
// Create a boundry...
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// Create body of the post
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"profile_photo_link\"; filename=\"ipodfile.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
UIImageView *imgvUserImage = [[UIImageView alloc]init];
imgvUserImage.image= [UIImage imageNamed:@"register_btn.png"];
NSData * imageData=[NSData dataWithData:UIImagePNGRepresentation(imgvUserImage.image)];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",txtUserName.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
postconnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
** For Response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self removeProgressIndicator];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == postconnection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
// NSDictionary *deserializedData = [responseString objectFromJSONString];
// NSString *string = [deserializedData objectForKey:@"Status"];
}
}