简体   繁体   English

将base64编码的图像上传到Mamp服务器

[英]upload base64 encoded image to Mamp server

I am sending image in base64 encoded format to server using following method 我正在使用以下方法将base64编码格式的图像发送到服务器

//for Upload Images...................... //用于上传图片......................

- (void)ImageUpload:(UIImage *)image
{
    NSData *imageData = UIImagePNGRepresentation(image);
    NSString *urlString=@"http://192.168.77.145/uploadImage.php";

    NSURL *url = [NSURL URLWithString:urlString];
    NSString *base64ImageString = [imageData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+

    base64ImageString = [base64ImageString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

    NSString *jsonReqString = [[NSString alloc] initWithFormat:@"{\"photo\":\"%@\",\"imageExtension\":\"%@\"}", base64ImageString,@"123"];


    NSString *post =[[NSString alloc] initWithFormat:@"request=%@",jsonReqString];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:postData];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!data)
        {
            NSLog(@"wifi error");
            NSLog(@"sendAsynchronousRequest error: %@", connectionError);

            return ;
        }
        else
        {
            NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            }
        }

    }];
}

but I didn't get base64 string to server. 但是我没有将base64字符串发送到服务器。 I got null both parameter photo and imageextension. 我的参数photo和imageextension都为空。

here is my server side code 这是我的服务器端代码

  <?php

$base = $_POST['photo'];
echo $base;
$filename = $_POST['imageExtension'];
echo $filename;
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');

$file = fopen('uploadedimages/'.$filename, 'wb');

// Create File
fwrite($file, $binary);
fclose($file);

echo 'Image upload complete, Please check your php file directory';
 ?>

Appreciate for help 感谢帮助

problem is on your second line of server side 问题在服务器端的第二行

your are trying to get value for key 'photo' 您正在尝试为关键“照片”获取价值

$base = $_POST['photo'];

you need to change that line 你需要改变那条线

  $base = $_POST['request'];

After that you need to add following code 之后,您需要添加以下代码

$baseImg = json_decode($base, true);
$filename = $baseImg["imageExtension"];
$tmpImg = $baseImg["photo"];

$binary=base64_decode($tmpImg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM