I\'m trying to send an audio file to a PHP server. Here is the Objective-C code:
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@\"File Size
In swift you can post like this..
func wsPostData(apiString: String, jsonData: String) -> Void
{
//apiString means base URL
let postData = jsonData.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
let postLenth = "\(UInt((postData?.length)!))"
let request = NSMutableURLRequest()
request.URL = NSURL(string: apiString)
request.HTTPMethod = "POST"
request.setValue(postLenth, forHTTPHeaderField: "Content-Lenth")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error:NSError?) in
print("response\(response)")
var dict:NSDictionary!
do {
dict = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
print(" responce data is \(dict)")
}
catch let error as NSError
{
print("Something went wrong\(error)")
}
}
}
Try encoding your binary data before you send it over the pipe (and then decode when it gets to the other end). See this SO thread for ideas for Base64 encoding code.
To debug: on the PHP side, try:
$file = $_POST['name'];
echo $file
Try the following code segment in place of your current code. Change the url (www.yourWebAddress.com) and script name to appropriate values for your problem.
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@"name=thefile&&filename=recording"];
[urlString appendFormat:@"%@", data];
NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *baseurl = @"https://www.yourWebAddress.com/yourServerScript.php";
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:postData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
[connection start];
NSLog(@"Started!");
On the server end, I have the following code:
<?php
$name = $_POST['name'];
$filename = $_POST['filename'];
echo "Name = '" . $name . "', filename = '" . $filename . "'.";
error_log ( "Name = '" . $name . "', filename = '" . $filename . "'." );
?>
I received the following output:
[23-May-2012 11:56:18] Name = 'thefile', filename = 'recording'.
I don't know why it is not working for you. You must have a missing step. Try commenting out the two lines in the url POST code above that references 'data' to see that you can get at the least the plain text for name and filename to your server end.
Good luck!