【问题标题】:Error sending http post request with image发送带有图像的 http post 请求时出错
【发布时间】:2012-05-29 16:14:53
【问题描述】:

我被这个问题困扰了好几天,我真的需要你的帮助。

我实际上是在尝试通过 php 脚本将文件从我的 iphone 应用程序上传到网络服务器。

这是我在 Xcode 中的方法:

-(void)sendPost{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"back.png"];

    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
    NSString *urlString = @"myscriptishere.php";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setTimeoutInterval:60.0];
    [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 stringWithString:@"Content-Disposition: form-data; name=\"userfile\";filename=\"myfile.png\"\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]];

    [request setHTTPBody:body];
    NSLog(@"%@",[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]);
    // Do any additional setup after loading the view.

}

我的php代码是:

<?php
    $domainpath = $_SERVER['DOCUMENT_ROOT'];
    $target = "./upload";
    $public = "/public/upload/";
    $target = $target.basename( $_FILES['userfile']['name']) ;
    $check = getimagesize($_FILES['userfile']['tmp_name']);

    if (move_uploaded_file($_FILES["file"]["tmp_name"],
                           $domainpath. $public . $_FILES["file"]["name"]))
    echo "YES";

    else
    echo "NO";


    ?>

我总是收到(几秒钟后,所以我想我正在上传一些东西)服务器回复“NO”。

我已经检查了我对要写入的文件夹的权限。

关于问题可能隐藏在哪里的任何想法?

提前感谢

【问题讨论】:

  • 也许你搞砸了 $_FILES['userfile'] 和 $_FILES["file"] 键。

标签: iphone ios xcode http post


【解决方案1】:

你的问题在这里:

NSString *urlString = @"myscriptishere.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];

您正在发布到一个文件名。您的urlString 变量应包含完整地址,包括协议和域(如@"http://localhost/myscriptishere.php")。

【讨论】:

  • 显然我的意思是@"h.t.t.p://www.mywebsite.com/myscript.php"。我的应用成功连接到网站和脚本,但我收到“否”作为对“POST”的响应
猜你喜欢
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多