图像Base64未在JSON请求中正确发送

时间:2013-05-08 06:17:05

标签: php ios xcode json base64

我正在将基本64中的图像发送到php服务器的JSON POST请求中。响应来了。但是空格会被添加到base64字符串中。因此,当我尝试解码base64时,它会失败。无法理解问题所在。我正在使用iPad应用程序并从iPad模拟器发送请求。

如果您对可能出现的错误有任何想法,请告诉我。非常感谢。

以下是我用于发送请求的ASIRequest代码 -

ASIFormDataRequest *asiRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlForAPI]];
[asiRequest setTimeOutSeconds:200];

NSMutableDictionary *requestheaders =[[[NSMutableDictionary alloc]init] autorelease];
[requestheaders setValue:@"application/x-www-form-urlencoded; charset=utf-8" forKey:@"Content-Type"];

NSData *jsonData = [data JSONData];

NSString *string = [[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding] autorelease];
[asiRequest setRequestHeaders:requestheaders];
NSString *jsonString = [NSString stringWithFormat:@"REQUEST=%@",string];

NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];

[asiRequest setPostBody:[NSMutableData dataWithData:requestData]];

[asiRequest setShouldContinueWhenAppEntersBackground:YES];
[asiRequest setDelegate:self];

[asiRequest setRequestMethod:@"POST"];
[asiRequest setDidFailSelector:@selector(uploadFailed:)];
[asiRequest setDidFinishSelector:@selector(uploadFinished:)];

[asiRequest startSynchronous];

1 个答案:

答案 0 :(得分:0)

试试这个

它在顶部声明:

   static const char _base64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /Write this code when you pass the string.
         NSData *imageData =  UIImagePNGRepresentation(YourImageView.image);
         NSString *imageString = [self encodeBase64WithData:imageData];

  //  only pass this imageStirng using NSUTF8StringEncoding instead of UTF8String

没有其他参数只检查图像是否上传。它为我工作。

   YourUrl= [[YourUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding]



    - (NSString *)encodeBase64WithData:(NSData *)objData {
        const unsigned char * objRawData = [objData bytes];
        char * objPointer;
        char * strResult;

        // Get the Raw Data length and ensure we actually have data
        int intLength = [objData length];
        if (intLength == 0) return nil;

        // Setup the String-based Result placeholder and pointer within that placeholder
        strResult = (char *)calloc((((intLength + 2) / 3) * 4) + 1, sizeof(char));
        objPointer = strResult;

        // Iterate through everything
        while (intLength > 2) { // keep going until we have less than 24 bits
            *objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
            *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
            *objPointer++ = _base64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)];
            *objPointer++ = _base64EncodingTable[objRawData[2] & 0x3f];

            // we just handled 3 octets (24 bits) of data
            objRawData += 3;
            intLength -= 3;
        }

        // now deal with the tail end of things
        if (intLength != 0) {
            *objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
            if (intLength > 1) {
                *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
                *objPointer++ = _base64EncodingTable[(objRawData[1] & 0x0f) << 2];
                *objPointer++ = '=';
            } else {
                *objPointer++ = _base64EncodingTable[(objRawData[0] & 0x03) << 4];
                *objPointer++ = '=';
                *objPointer++ = '=';
            }
        }

        // Terminate the string-based result
        *objPointer = '\0';

        // Return the results as an NSString object
        return [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding];
    }
它可能对你有所帮助。并且在服务器上获取图像也有可能存在问题。