iphone - 发送带有图像的 http post 请求时出错

标签 iphone ios xcode http post

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

我实际上是在尝试通过 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";


    ?>

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

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

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

提前致谢

最佳答案

你的问题在这里:

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

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

关于iphone - 发送带有图像的 http post 请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802900/

相关文章:

javascript - 网络移动可靠高度

iphone - @synthesize IBOutlet 属性

iphone - 读取 grib2 数据

iphone - UIBezierPath 未在 drawRect 中绘制

objective-c - Bool 值的自定义访问器

ios - 滑动 View Controller ,但不使用 UISwipeGestureRecognizer

iphone - 在 iPhone 中获取地址信息

ios - 将 WCSession 与多个 ViewController 一起使用

ios - 返回组合字典的字典扩展

objective-c - 在虚拟键盘中单击“下一步”按钮期间从一个文本字段移动到另一个文本字段