10

如何通过相同的 http post 请求将一些文本信息(文本字符串)和图像文件上传到服务器。我自己上传了图像,但无法使用文本。谢谢!

4

4 回答 4

19

使用此代码上传图片和文本标签

NSData *imageData = UIImageJPEGRepresentation("yourImage",0.2);     //change Image to NSData

if (imageData != nil)
{
    filenames = [NSString stringWithFormat:@"TextLabel"];      //set name here
        NSLog(@"%@", filenames);
    NSString *urlString = @"http://xxxxxxx/yyyyy.php";

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(returnString);
    NSLog(@"finish");
}

在 php 端使用此代码

$myparam = $_POST['userfile'];     //getting image Here
$mytextLabel= $_POST['filenames']   //getting textLabe Here
echo $myparam;
echo $mytextLabel; 
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['myfile']['name']);  

if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['myfile']['name']) . " has been uploaded";
} else {
    echo "There was an error uploading the file, please try again!";
}
于 2012-06-29T13:01:18.553 回答
4

只需在单击按钮上添加以下方法:

-(void) uploadImage
{
    prodNam = txtProdName.text;
    UIImage * img = [UIImage imageNamed:@"SRT2.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(img,0.2);     //change Image to NSData

    if (imageData != nil)

    {
        NSString * filenames = [NSString stringWithFormat:@"TextLabel"];
        NSLog(@"%@", filenames);

        NSString *urlString = @"http://dev9.edisbest.com/upload_image.php";

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"TestEdreamzIpad.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"Response : %@",returnString);

        if([returnString isEqualToString:@"Success ! The file has been uploaded"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

            [alert show];
        }
    NSLog(@"Finish");
    }
}

您可以将任何图像放在 xcode 本地层次结构的“SRT2.jpg”位置。

一切顺利...

于 2013-02-19T09:49:35.990 回答
2

只需用户SVHttp 请求将文件上传到服务器。

在参数中发送 imageFile 和其他东西:

NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
    [data setObject:imageData forKey:@"image"];
    [data setObject:@"saveimage" forKey:@"action"];
    [data setObject:userId forKey:@"userid"];
    [data setObject:@"png" forKey:@"type"];

并提出请求:

SVHTTPClient *request = [SVHTTPClient sharedClient];

    [request setBasicAuthWithUsername:nil password:nil];
    [request setSendParametersAsJSON:NO];



    [request POST:[NSString stringWithFormat:@"http://bylyngo.com/getapi/?action=saveimage&userid=%@&type=png",userId]
       parameters:data
         progress:^(float progress) {

             NSLog(@"Uploading (%.0f%%)", progress*100);

         }];

你甚至会得到进步。

:)

于 2016-02-08T07:12:50.680 回答
1

我的网络服务就是这个,它运行良好!

<?php


$fp = fopen("./trace.txt", "w"); //creates a file to trace your data
fwrite($fp,"get \n");
fwrite($fp, print_r($_GET, true));
fwrite($fp,"POST \n");
fwrite($fp, print_r($_POST, true));//displays the POST 
fwrite($fp,"FILES \n");
fwrite($fp,print_r($_FILES,true));//display the FILES 
fclose($fp);


$myparam = $_POST["userfile"];
$mytextLabel = $_POST['filenames'];
echo $myparam;
echo $mytextLabel;


$uploadDir = 'uploads/'; //you must create this directory
$uploadDir = $uploadDir.basename($_FILES['myfile']['name']); //saves the picture inside that folder


$file = basename($_FILES['uploaded']['name']);
//$uploadedFile = $uploadDir.$file;
//move_uploaded_file($_FILES['uploaded']['tpm_name']);


if(move_uploaded_file($_FILES['myfile']['tpm_name'],$uploadDir)){
echo "the file ".basename($_FILES['myfile']['name'])." has been uploaded" ;
}else{

echo "error";
} 

?>
于 2014-01-08T04:50:58.213 回答