图像 Base64 未在 JSON 请求中正确发送
Posted
技术标签:
【中文标题】图像 Base64 未在 JSON 请求中正确发送【英文标题】:Image Base64 not getting Sent Correctly in JSON Request 【发布时间】:2013-05-08 06:17:05 【问题描述】:我正在将 JSON POST 请求中的 base64 图像发送到 php 服务器。响应来了。但是空格被添加到 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];
【问题讨论】:
我们能看到一个发布请求的例子以及服务器正在接收什么吗?var_dump($_POST)
.
@SAMIRRATHOD 我已经用代码更新了问题。请检查。
【参考方案1】:
试试这个
它在顶部声明:
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];
它可以帮助你。并且在服务器上获取图像可能存在问题。
【讨论】:
以上是关于图像 Base64 未在 JSON 请求中正确发送的主要内容,如果未能解决你的问题,请参考以下文章
使用 JSON 从 Android 发送 Base64 图像到 php webservice,解码,保存到 SQL
通过 JSON 将 base64 的图像发送到 PHP GD 库