我有一個應用程序,可以在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