I am trying to upload the image and textfield data to the server. Now, I can upload the textfield data to the server successfully. However, I do not know how to upload the image and textfield data simultaneously. Can anyone help me?
Here is the code:
-(IBAction)submit:(id)sender{
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *date = self.date.text;
NSString *phone = self.phone.text;
NSString *email = self.email.text;
NSString *address = self.address.text;
NSString *contact = self.contact.text;
NSMutableString *rawStr = [NSMutableString stringWithFormat:@"date=%@&@&phone=%@&email=%@&address=%@&contact=%@", date,
phone,email,address,contact];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost/abc/savedata.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"%@", responseString);
NSString *success = @"success";
[success dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%lu", (unsigned long)responseString.length);
NSLog(@"%lu", (unsigned long)success.length);}
Here is my PHP script:
<?php
header('Content-type: text/plain; charset=utf-8');
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "newmy";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_query("SET NAMES UTF8");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
$strdate = $_POST["date"];
$strphone = $_POST["phone"];
$stremail = $_POST["email"];
$straddress = $_POST["address"];
$strcontact = $_POST["contact"];
/*** Insert ***/
$strSQL = "INSERT INTO repairform (datedate,phone,email,address,contact)
VALUES (
'".$strdate."',
'".$strphone."',
'".$stremail."',
'".$straddress."',
'".$strcontact."'
)
";
$objQuery = mysql_query($strSQL);
$arr = null;
if(!$objQuery)
{
$arr["Status"] = "0";
$arr["Message"] = "Insert Data Failed";
}
else
{
$arr["Status"] = "1";
$arr["Message"] = "Insert Data Successfully";
}
echo json_encode($arr);
?>
答案 0 :(得分:1)
This is the function I have used to post image and data together
- (void)postRequestForService:(NSString *)service withParams:(NSDictionary *)params
{
NSString *strURL=[NSString stringWithFormat:@"%@%@",BASE_URL,service];//strURL is actually the entire url for the webservice
NSString *boundaryString = @"0xKhTmLbOuNdArY";
NSData *boundaryData = [[NSString stringWithFormat:@"\r\n--%@\r\n", boundaryString] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setTimeoutInterval:30];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[theRequest setHTTPShouldHandleCookies:NO];
[theRequest setURL:[NSURL URLWithString:strURL]];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundaryString] forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
//params is a NSDictionary that contain post request parameters name and value as associated key and key value
for (NSString *key in [params allKeys])
{
[body appendData:boundaryData];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[params[key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:boundaryData];
}
// setting the body of the post to the reqeust
[theRequest setHTTPBody:body];
//create the connection
connection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}