views:

56

answers:

2

Hey, am writing an application and I need to convert a .xls file to xml to feed it into a web service. All my google searches involve converting it by hand. I've not been able to find any tutorials on how to do this automatically through iOS, does anyone know of a good tutorial or even a book that includes this?

Thanks, William

A: 

You've got two choices; convert it to .xlsx and most of the work's done for you, or if you plan on simply 'unpacking' the .xls at the other end, you could just do <data type="application/vnd.ms-excel">(Base64encoded file contents)</data>, or something similar.

Flynn1179
Thanks for the reply, am new to web services and transferring data to and from web services. How would i base64encode by file?
wibo
Depends on your platform obviously, but here's the method in .NET that does it: http://msdn.microsoft.com/en-us/library/dhx0d524.aspx
Flynn1179
A: 

After much searching I managed to find a solution where I send the .xls file as an .xls file like Flynn1179 suggested. I got confused into thinking that I had to send the file as xml as it was to a web service. Below is the code I used:

-(void)uploadFile:(NSString*)filename withExtension:(NSString*)extension{
    NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:extension];

    NSData *data = [NSData dataWithContentsOfFile:path];

    NSString *urlString = @"http://server/path/to/webservice/call";
    NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"%@.%@\"\r\n", filename, extension] dataUsingEncoding:NSUTF8StringEncoding]]; //"uploadedfile" should be substituted for whatever variable the backend script expects the file variable to be
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"returned string: %@", returnString); //Just for display purposes

}

Thanks for everyones help

wibo