簡體   English   中英

如何使用POST上傳圖片和文字

[英]How to upload the image and text using 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);
 ?>

這是我用來一起發布圖像和數據的功能

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM