I am sending an image in base64 in JSON POST request to php server. Response comes Ok. But spaces are getting added to the base64 string. So when I try to decode the base64, it fails. Could not understand where the problem is. I am working on an iPad app and sending the request from iPad simulator.
我发送一个图像在JSON base64 POST请求到php服务器。反应是好的。但是空间添加到base64字符串。所以当我试图base64解码,它失败了。不明白问题在哪里。我工作在一个iPad应用程序,从iPad模拟器发送请求。
Let me know if you even have slightest idea of what could be wrong. Thanks much.
如果你对什么可能是错的,请告诉我。谢谢。
Following is the ASIRequest code I am using for sending the request -
以下是我用来发送请求的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 个解决方案
#1
0
try this
试试这个
it declare on top:
它声明顶部:
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
no other parameter just check image is uploaded or not. its work for me.
没有其他参数只是检查图像是否上传。它为我工作。
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];
}
it may help you. and also it's possible there is problem with getting image on server.
它可以帮助你。而且,在服务器上获取图像也有问题。
#1
0
try this
试试这个
it declare on top:
它声明顶部:
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
no other parameter just check image is uploaded or not. its work for me.
没有其他参数只是检查图像是否上传。它为我工作。
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];
}
it may help you. and also it's possible there is problem with getting image on server.
它可以帮助你。而且,在服务器上获取图像也有问题。