milanjansari
milanjansari

Reputation: 1549

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><cdate>%@</cdate><file>%c</file><fname>File</fname>",fileSizeVal,filetype,creationDate,myData];

Any idea about this?

Thanks you, Milan

Upvotes: 0

Views: 1797

Answers (3)

TechZen
TechZen

Reputation: 64428

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.

Upvotes: 1

Stefan Arentz
Stefan Arentz

Reputation: 34945

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

Upvotes: 1

Iggy
Iggy

Reputation: 8669

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

Upvotes: 0

Related Questions