我对iOS很新,一般都很快,希望有人可以提供帮助。我正在尝试创建一个非常简单的快速iOS应用程序,它将使用多个参数执行HTTP帖子,其中一个参数是图像或音频文件。如果你看到一个,请建议更好的方法,但这只是一个概念证明。显然我正在发布,所以它不能从我的swift应用程序工作,但我已经能够通过其他方式(即“Rest Client”ios应用程序)使PHP Web服务工作。
func uploadFile(filename:String) {
println("uploading \(filename)")
var docsDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var url = NSURL(fileURLWithPath: docsDir + "/" + filename)
var request = NSMutableURLRequest(URL: NSURL(string: "http://www.somewebsite.com/testWS.php"))
var audioData :NSData = NSData.dataWithContentsOfURL(url,options: NSDataReadingOptions.DataReadingMappedIfSafe,error: nil)
var bodyData: String = "filenames=Tesing123&userfile=\(audioData)"
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
}
我的PHP网络服务是:
<?php
$myparam = $_POST['userfile']; //getting image Here
$mytextLabel= $_POST['filenames']; //getting textLabe Here
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['userfile']['tmp_name']);
echo basename( $_FILES['userfile']['name']);
echo $_POST['userfile'];
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['userfile']['name']) . " has been uploaded";
} else {
echo "!!!!!There was an error uploading the file, please try again!";
}
?>
提前致谢。