将照片从iphone上传到网络服务器

时间:2012-04-21 15:28:37

标签: php iphone ios upload webserver

我试图通过使用许多不同的.php上传脚本将我的iPhone应用中的照片上传到我的webserver,但到目前为止我没有取得任何成功!

请查看我的iPhone源代码:

- (IBAction)upload:(id)sender {
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
    NSString *urlString = @"http://wasnaga.nestons.org/420/upload.php";

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

    NSString *boundary = [NSString stringWithString:@"------------------------1473780983146499882746641449"];
    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=\"userfile\"; filename=\.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"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(returnString);   
}

有任何错误吗?你能帮我看看.php文件吗?

1 个答案:

答案 0 :(得分:0)

看起来你在第一个appendData行中缺少反斜杠:

@"\r\n--%@r\n"

应该是:

@"\r\n--%@\r\n"