我想从我的iPhone应用程序发送一个小图像以及一些XML信息到App Engine。我在路上的某个地方遇到了问题。没有错误,并且数据正在被转移到数据存储查看器中的Blob条目中,但App Engine上的blob文件未出现在Blob查看器中。我怀疑图片在App Engine中的某个转换中混乱,或者未被存储为App Engine中的正确类型。你怎么看?图像通过XML从iPhone发送到App Engine
在iPhone上,这里是有关部分编码图像(使用标准base64Encoding功能),并将其添加到GDataXMLElement,然后把它加入到一个GDataXMLDoc和发送与ASIHTTPRequest:
NSString *dataString = [self.data base64Encoding];
GDataXMLElement *tempXMLElement = [GDataXMLElement elementWithName:@"data" stringValue: dataString];
[imageElement addChild: tempXMLElement];
在ASIHTTPRequest部分:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: url];
[request setDelegate: self];
[request addRequestHeader:@"User-Agent" value: [NSString stringWithFormat:@"MyApp:%@", version]];
[request addRequestHeader:@"Content-Type" value:@"text/xml"];
[request setShouldStreamPostDataFromDisk:YES];
[request appendPostDataFromFile: path];
[request start];
在App Engine上的Python(最有可能在那里一个问题在于):
image_data_xml_element = image_xml_node.getElementsByTagName("data")[0]
image_data_base64_unicode = image_data_xml_element.firstChild.data
image_data_base64_ascii = image_data_unicode.encode("utf8")
image_data_string = binascii.a2b_base64(image_data_base64_ascii)
new_image.data = db.Blob(image_data_string)
此外,我曾尝试:
image_data_xml_element = image_xml_node.getElementsByTagName("data")[0]
image_data_base64_unicode = image_data_xml_element.firstChild.data
image_data_string = base64.b64decode(image_data_base64_unicode)
new_image.data = db.Blob(image_data_string)
编辑:为了完整起见,这里是我使用的Objective-C的base64库 - 它看起来并不像我所期待的:
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/";
- (NSString *)base64Encoding;{
if ([self length] == 0)
return @"";
char *characters = malloc((([self length] + 2)/3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [self length]){
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [self length])
buffer[bufferLength++] = ((char *)[self bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}
谢谢,有道理。现在我不会将他们从Blob Viewer中缺席作为反馈。 – 2010-09-12 00:32:03