Monday, October 10, 2011

Uploading XML to server via PHP

XCODE CODE
_____________________
 
            // #######################################################         
           
            // Uploading to .net SERVER  ####################################################### 
           
           
           
            // #######################################################           
           
           

           
           
           
            //
          
           
            NSString *urlString = @"http://www.innokria.com/dotnet.php";
            NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
            [request setURL:[NSURL URLWithString:urlString]];
            [request setHTTPMethod:@"POST"];
           
            NSString *boundary = [NSString stringWithString:@"0xLhTaLbOkNdArZ"];
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
            [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
           
            //Reading the file
            NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"readme.xml"];
            NSData *myData = [NSData dataWithContentsOfFile:Read];
             NSLog(@"file exist at %@",myData);
            NSMutableData *body = [NSMutableData data];
           
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"dotnet\"; filename=\"test.xml\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:myData];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
           
            [request setHTTPBody:body];
           
            NSError *returnError = nil;
            NSHTTPURLResponse *returnResponse = nil;
           
            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnResponse error:&returnError];
           
           
           
           
           
           
##################################

in php  , make a empty  folder called dotnets and upload this php page


_______________dotnet.php

$uploaddir = './dotnets/';
$file = basename($_FILES['dotnet']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['dotnet']['tmp_name'], $uploadfile)) {
        echo "http://www.innokria.com/dotnet/{$file}";
}
?>






FEEDJIT Live