从PHP到.NET WCF服务发布字节数组

我有一个接收文件的方法的WCF服务,看起来像这样

 public bool UploadFile(string fileName, byte[] data) { //... } 

我想做的是在PHP的WCF服务中将数据发布到此方法,但是如果甚至可以将字节数组从PHP发布到由WCF服务托管的.NET方法,则不知道。

所以我在考虑这样的事情

 $file = file_get_contents($_FILES['Filedata']['tmp_name']); // get the file content $client = new SoapClient('http://localhost:8000/service?wsdl'); $params = array( 'fileName' => 'whatever', 'data' => $file ); $client->UploadFile($params); 

这是可能的,还是有任何一般的建议,我应该知道吗?

弄清楚了。 官方php文档告诉file_get_contents将整个文件作为字符串返回(http://php.net/manual/en/function.file-get-contents.php)。 没有人告诉的是,当发布到WCF服务时,此字符串与.NET bytearray兼容。

见下面的例子。

 $filename = $_FILES["file"]["name"]; $byteArr = file_get_contents($_FILES['file']['tmp_name']); try { $wsdloptions = array( 'soap_version' => constant('WSDL_SOAP_VERSION'), 'exceptions' => constant('WSDL_EXCEPTIONS'), 'trace' => constant('WSDL_TRACE') ); $client = new SoapClient(constant('DEFAULT_WSDL'), $wsdloptions); $args = array( 'file' => $filename, 'data' => $byteArr ); $uploadFile = $client->UploadFile($args)->UploadFileResult; if($uploadFile == 1) { echo "

Success!

"; echo "

SharePoint received your file!

"; } else { echo "

Darn!

"; echo "

SharePoint could not receive your file.

"; } } catch (Exception $exc) { echo "

Oh darn, something failed!

"; echo "

$exc->getTraceAsString()

"; }

干杯!