2016-08-31 11 views
0

画像とテキストフィールドのデータをサーバーにアップロードしようとしています。これで、テキストフィールドのデータをサーバーに正常にアップロードできます。しかし、イメージとテキストフィールドのデータを同時にアップロードする方法はわかりません。誰でも助けてくれますか?ここでPOSTを使用して画像とテキストをアップロードする方法

はコードです:ここでは

-(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);} 

は私のPHPスクリプトです:

<?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); 
?> 
+2

を画像とデータをポストするために使用されている機能です警告**:PHPを学んでいるだけの方は、['mysql_query'](http://php.net/manual/en/function.mysql-query.php)インタフェースを使用しないでください。それはPHP 7で削除されたのでとてもひどいと危険です。[PDOのようなものは学ぶのが難しくない](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)と[PHP The Right Way](http://www.phptherightway.com/)のようなガイドがベストプラクティスを説明しています。あなたのユーザーパラメータは**適切にエスケープされていません**(http://bobby-tables.com/php)、悪用可能な[SQLインジェクションバグ](http://bobby-tables.com/)があります。 – tadman

+0

あなたのリマインダーとコメントありがとう – user3472143

答えて

1

これは私が**一緒

- (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]; 
} 
+0

あなたの答えをありがとう。あなたのコードで、私は画像が投稿を使用していることを知ることができます。テキスト(テキストフィールドデータ)をどこに追加するか教えていただけますか? PHPスクリプトを提供していただけますか?ありがとうございました。 – user3472143

関連する問題