Hamza Azad
Hamza Azad

Reputation: 2637

iOS - How to send attachment using SOAP?

I am using NSMutableURLRequest to upload parameters to server using SOAP API and it is working fine.

static NSString *URLString = @"http://www.myURL";
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLString]];

[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"]; 
[request addValue:@"http://..." forHTTPHeaderField:@"SOAPAction"];

NSString *name = @"John";

NSString *parameters = [NSString stringWithFormat:
                         @"<s:Envelope xmlns:s=\"http://..../\">\n"
                         "<s:Body >\n"
                         "<name>%@</name>\n"
                         "</s:Body>\n"
                         "</s:Envelope>\n", name];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

_myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Now I want to send a text file (attachment) along with the parameters.

Can someone help?

Upvotes: 0

Views: 441

Answers (1)

abhishekkharwar
abhishekkharwar

Reputation: 3529

Hi you can convert your file into NSDATA then into BASE64 String and add as parameter to your request body and after that at backend you can convert BASE64 String to Binary Data and save it as text file.

Encoding

NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"file.txt"];
NSData *data = [NSData dataWithContentsOfFile:str];
NSString *base64String = [data base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); 

Here is the link for BASE64 Conversion classes

Just add one more parameter

NSString *parameters = [NSString stringWithFormat:
                     @"<s:Envelope xmlns:s=\"http://..../\">\n"
                     "<s:Body >\n"
                     "<name>%@</name>\n"
                     "<fileData>%@</fileData>\n"   //Here is new parameter
                     "</GetChanges>\n"
                     "</s:Body>\n"
                     "</s:Envelope>\n", name, base64String];

Upvotes: 1

Related Questions