【问题标题】:post image and other data using mulipart form data in iphone在 iphone 中使用多部分表单数据发布图像和其他数据
【发布时间】:2010-03-31 11:36:42
【问题描述】:

我正在使用目标 C 中的 multipart/form-data 向服务器发送一些数据和图像。 请给我一些 PHP 代码,我如何将图像保存在服务器上,我能够在服务器上获取与图像一起传递的其他变量。请查看我的 obj C 代码和 php 并告诉我哪里错了。

我们将非常感谢您的帮助。

我在这里发出 POST 请求。

////////////////////

    NSString            *stringBoundary, *contentType, *baseURLString, *urlString;
    NSData              *imageData;
    NSURL               *url;
    NSMutableURLRequest *urlRequest;
    NSMutableData       *postBody;

    // Create POST request from message, imageData, username and password
    baseURLString   = @"http://localhost:8888/Test.php";
    urlString       = [NSString stringWithFormat:@"%@", baseURLString];  
    url             = [NSURL URLWithString:urlString];
    urlRequest      = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
    [urlRequest setHTTPMethod:@"POST"]; 

    // Set the params
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LibraryIcon" ofType:@"png"];
    imageData = [[NSData alloc] initWithContentsOfFile:path];

    // Setup POST body
    stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    contentType    = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    [urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

    // Setting up the POST request's multipart/form-data body
    postBody = [NSMutableData data];

    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]];  // So Light Table show up as source in Twitter post

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:book.title] dataUsingEncoding:NSUTF8StringEncoding]];  // title

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"isbn\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:book.isbn] dataUsingEncoding:NSUTF8StringEncoding]];  // isbn

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"price\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:txtPrice.text] dataUsingEncoding:NSUTF8StringEncoding]];  // Price

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"condition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:txtCondition.text] dataUsingEncoding:NSUTF8StringEncoding]];  // Price


    NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"\r\n\n\n"]dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [postBody appendData:imageData];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


//  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"postBody=%@", [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding]);
    [urlRequest setHTTPBody:postBody];
    NSLog(@"Image data=%@",[[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding]);

    // Spawn a new thread so the UI isn't blocked while we're uploading the image
    [NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest]; 

我的方法 uploadingDataWithURLRequest 我将请求发布到服务器...

这是我的php代码

?php
    $title = $_POST['title'];
    $isbn  = $_POST['isbn'];
$price = $_POST['price'];
$condition = $_POST['condition'];
$image=$_FILES['image']['name'];



if($image)
{
    $filename = 'newimage.jpeg';
    file_put_contents($filename, $image);
    echo "image is there";
}
else
{
 echo "image is nil";
}


?> 

我无法在服务器上获取图像,请帮助我在哪里出错。

【问题讨论】:

  • 我有一个代码,我只将图像发送到服务器。在服务器上必须有“上传”目录,我的代码会将图像发送到 php 文件,然后 php 将该文件上传到“上传”目录......我不知道如何在此处添加文件或者如果有任何方法....管理员建议我解决方案或在 bharat.jagtap@bitcode.in 上给我发一封测试邮件,我会将这些文件发送给您...

标签: php iphone objective-c


【解决方案1】:

您访问上传的文件不正确。 $_FILE['image']['name'] 是客户端上传的文件名。您正在做的是将该名称(一个简单的字符串,例如“test.jpg”)写入 $filename,因此您的“newimage.jpeg”文件包含一个字符串,而不是 JPEG 数据。

你想要的是以下内容:

move_uploaded_file($filename, $_FILES['image']['tmp_name']);

$_FILES 数据的“tmp_name”部分是文件临时存储在服务器上的绝对路径(例如“/var/tmp/sometemporaryuglyfilename”),然后您可以使用move_uploaded_file().

【讨论】:

  • 你的意思是我应该写 $image=$_FILES['image']['tmp_name'];而不是 $image=$_FILES['image']['name'];我现在更改了代码,现在我的代码是 即使现在它也不起作用,你能告诉我我的 obj C 代码是否正确。
  • 它是move_uploaded_file(DESTINATION, SOURCE)。你有你的倒退。不能帮助你实现目标 C。从来没有碰过这些东西。但是您可以轻松地将一些调试/日志记录代码放入您的 PHP 中,以查看数据和文件是否正确发布。
猜你喜欢
  • 2016-04-06
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2014-08-06
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多