从IOS到php服务的Base64图像

时间:2013-12-28 16:17:22

标签: php ios service web escaping

如果我将图像发送到php服务器,仅在base64中,由于特殊字符(例如+ * /),传输百分比几乎为零。 如果我发送它已成功发送,但图像在服务器上没有数据出现。有人帮忙解决这个问题,一边是php吗?

string = [imageData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+

NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (CFStringRef)string,
                                                                                  NULL,
                                                                                  CFSTR("!*'();:@&=+$,/?%#[]\" "),
                                                                                  kCFStringEncodingUTF8));

NSString * urlString2 = [NSString stringWithFormat:@“mylink.com/xyz”];

NSString *jsonString2= [NSString stringWithFormat: @"{\"id_sala\":\"%@\",\"base\":\"%@\",\"tipo\":\"IOS\"}",iddsala,encodedString];
 NSLog(@"%@",encodedString);

NSData* responseData2 = nil;
NSURL *url2=[NSURL URLWithString:[urlString2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
responseData2 = [NSMutableData data] ;
NSMutableURLRequest *request2=[NSMutableURLRequest requestWithURL:url2];
NSString *bodydata2=[NSString stringWithFormat:@"%@",jsonString2];

[request2 setHTTPMethod:@"POST"];
NSData *req2=[NSData dataWithBytes:[bodydata2 UTF8String] length:[bodydata2 length]];
[request2 setHTTPBody:req2];
NSURLResponse* response2;
NSError* error2 = nil;
responseData2 = [NSURLConnection sendSynchronousRequest:request2     returningResponse:&response2 error:&error2];
responseString2 = [[NSString alloc] initWithData:responseData2 encoding:NSUTF8StringEncoding];

2 个答案:

答案 0 :(得分:0)

作为问题的解决方案,请尝试使用php脚本中的urldecode(http://www.php.net/manual/en/function.urldecode.php)解码base64编码的图像数据。

然后使用file_put_contents(http://www.php.net/manual/en/function.file-put-contents.php

将图像数据写入文件

另请尝试执行示例代码段

E.g

      $img_data=urldecode(base64_decode($img_data));
      file_put_contents($filename,$img_data);

答案 1 :(得分:0)

有几点意见:

  1. 您应该使用NSJSONSerialization来构建JSON。使用stringWithFormat手动执行此操作只会引入潜在问题。

  2. 不要将百分比转义为base64字符串。 NSJSONSerialization将自动确保JSON格式正确。

  3. 与原始问题无关,但最佳做法是设置Content-Type标题以反映请求正文的详细信息。

  4. 也无关,但你真的应该使用异步请求。您永远不应该从主队列发出同步网络请求。

  5. 因此:

    NSURL *url = [NSURL URLWithString:@"http://mylink.com/xyz"]; // don't forget http; also all of those `stringWithFormat` references building urlString2 was entirely unnecessary
    
    NSString *base64String = [imageData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+
    
    NSDictionary *dictionary = @{@"id_sala" : iddsala,
                                 @"base"    : base64String,
                                 @"tipo"    : @"IOS"};
    NSError *error;
    NSData *httpBody = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
    NSAssert(httpBody, @"dataWithJSONObject error: %@", error);
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:httpBody];
    
    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!data || error) {
            NSLog(@"sendAsynchronousRequest error: %@", error);
            return;
        }
    
        // use the `data` result here; e.g. if JSON response
    
        NSError *parseError;
        id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
        if (responseObject) {
            NSLog(@"responseObject = %@", responseObject);
        } else {
            // some problem parsing JSON; it's often useful to keep at the body of the response to diagnose problem
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"responseString = %@", responseString);
        }
    }];
    [task resume];