【发布时间】:2014-12-04 08:02:07
【问题描述】:
我有一个应用程序,我在网络服务器上上传用户数据。
我正在使用下面的代码通过下面的代码将数据传递给网络服务器。
-(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);
}
}
我可以在没有图像的情况下成功地将数据发送到网络服务器。
但是如果使用上面显示的代码通过Base64编码上传图像,则数据不会上传到网络服务器。 在 NSLog 中显示请求查询字符串太长。
我已经导入了 Base64.h 和 .m 文件。
我也想通过图像传递数据。
我该怎么做?
我知道有很多链接,但我想以这种方式传递它,所以请告诉我我在这里做错了什么?
请帮帮我。
提前致谢。
【问题讨论】:
标签: php ios objective-c image webserver