我有一个应用程序,可以在Web服务器上上传用户数据。使用PHP在ios上通过JSON在web服务器上传图像
我正在使用下面的代码将数据传递给webserver与下面的代码。
-(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",@"[email protected]",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);
}
}
我成功地能够发送数据到网络服务器没有图像。
但是,如果通过编码Base64上传带有上述代码的图像,则数据不会上传到网络服务器上。 它给我一个错误在NSLog说请求查询字符串太长。
我已经导入Base64.h和.m文件。
我想通过图像传递数据也。
我该怎么做?
我知道有很多链接,但我想通过这种方式,所以请告诉我我在这里做什么错了?
请帮帮我。
在此先感谢。
感谢哥们。我会检查它并让你知道。 – Manthan 2014-10-10 05:33:51
我仍然得到请求字符串的错误信息太长,因为我在NSLog中得到了太长的base64String。 – Manthan 2014-10-10 05:48:05
你是否知道如何通过多路径数据发送图像?如果是的话,请帮助。免费帮助kar ne bhai。 – Manthan 2014-10-21 06:48:21