tags:

views:

490

answers:

4

Hello,

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

A: 

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

Iggy
Hello,i am frustrate can you send me the code so i will use Thakns you,
milanjansari
+1  A: 

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.

TechZen
Hello,sorry for i dont know how to use this - (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding in my code.Thanks you,
milanjansari
See edit for detailed explanation. Don't forget to hit the checkmark next to the answer that best solves your problem.
TechZen
Thanks my problem is solved.I am very thankful to all
milanjansari
A: 

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

St3fan
A: 

Hello,

i have one pdf,doc file i want to get pdf data ane insert in mysql using webservice

NSFileManager fileManager = [NSFileManager defaultManager]; NSError error = nil; NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"docx"];

NSData *myData = [NSData dataWithContentsOfFile:pathDoc];

NSString *soapMessage = [NSString stringWithFormat: @"\n" " \n" "\n" "\n" "%@\n" "\n"
"\n" "\n",myData];

So how can i pass binary data using webservice

its urgent can anyone help me out. Thanks you

milanjansari