如何将发布数据和图像文件发送到服务器Xcode [英] How to send post data and image file to server Xcode

查看:98
本文介绍了如何将发布数据和图像文件发送到服务器Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I upload some text info (a text string) and an image file over the same http post request to server. I got images upload by itself, but can't get text to work with it. Thanks!

推荐答案

使用此代码上传图片和textLabel

Use this code to upload image and textLabel

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!";
}

这篇关于如何将发布数据和图像文件发送到服务器Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆