开发者

iPhone: How to Embed NSData in XML for Upload to Server?

I want to pass data to a server and store the file there in a database as binary data.

NSData *myData = [NSData dataWithContentsOfFile:pathDoc]; 
pathDoc = [NSString stringWithFormat:@"<size>%d</size><type>%d</type>开发者_StackOverflow<cdate>%@</cdate><file>%c</file><fname>File</fname>",fileSizeVal,filetype,creationDate,myData];

Any idea about this?

Thanks you, Milan


The most common way to embed binary data in an XML document is to encode the data to ASCII. For example using Base64.


This code makes no sense.

You create a NSData object with a path to a file then you turn around and reassign to the path var a string representing a chunk of XML. It's very muddled and likely to cause errors. Don't reuse variables this way.

In any case, to encode data to a string you use NSString's:

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

Then just insert that string wherever you want it.

Edit:

Hello, sorry for i dont know how to use this - (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding in my code.

Like so: (Sorry about the formatting Stackoverflow's editor seems to be on blink today)

NSData *myData = [NSData dataWithContentsOfFile:pathDoc]; 
NSString *myDataString = [[NSString alloc] initWithData:myData encoding:NSUnicodeStringEncoding];
NSString *formatString = @"<size>%d</size><type>%d</type><cdate>%@</cdate><file>%c</file><fname>File</fname><data>%@</data>";
pathDoc = [NSString stringWithFormat:formatString,fileSizeVal,filetype,creationDate,myDataString];

The NSUnicodeStringEncoding is one of several constants defining a variety of string encodings. See the NSString class reference. Which one you use depends on what your server expects.

Good luck.


you should be able to use object archiving to create a serialized object and then send that to the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜