我正在開發一個iphone應用程序並使用POST請求,我已經閱讀了許多帖子,但是仍然不能正常工作,我會感謝一些幫助,這裏是我的代碼:使用Objective C上傳圖像到PHP服務器
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSArray *json_data = [NSJSONSerialization JSONObjectWithData:web_data options:kNilOptions error:NULL];
NSString *returningString = [[NSString alloc] initWithData:web_data encoding:NSUTF8StringEncoding];
NSLog(@"returned string = %@", returningString);
[loadingView stopLoading];
}
我的PHP端,只告訴我,它以這種方式獲得:
//turning the image into a NSData object
//getting the image back out of the UIImageView
//setting the quality to 72
NSData *image_data = UIImageJPEGRepresentation(picture, 72);
// setting up the URL to post to
NSMutableString *urlString = [[NSMutableString alloc] init];
[urlString appendString:BASE_URL];
[urlString appendString:@"mobile_services/mobile_store_services/upload_wishlist_image/"];
//[urlString appendString:@"/temp_trash/temporal.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"test.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:image_data];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSString *param1 = @"parameter text";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"texto_prueba\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn){
web_data = [NSMutableData data];
}else{
NSLog(@"doesn't exist");
}
我使用NSURLConnectionDataDelegate協議的話,我在這個方法中接收到的數據
public function upload_wishlist_image()
{
var_dump($_REQUEST);
}
請幫忙!,PHP沒有收到任何圖片數據。
我已經找到了解決方案,它只是將NSData編碼爲base64,將它作爲一個普通的NSString傳遞給它的主體,它完美地工作。 –