2012-06-06 56 views
0

我使用WSO2 WS Framework Web服务接收到,我设法运行示例,其中Web服务返回的图像作为MTOM附件,然后由客户端使用file_put_contents保存(...)命令。PHP“保存对话框”文件从

服务:

<?php 

function sendAttachment($msg){ 
$responsePayloadString = <<<XML 
    <ns1:download xmlns:ns1="http://wso2.org/wsfphp/samples/mtom"> 
     <ns1:fileName>test.jpg</ns1:fileName> 
      <ns1:image xmlmime:contentType="image/jpeg" xmlns:xmlmime="http://www.w3.org/2004/06/xmlmime"> 
       <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:myid1"></xop:Include> 
      </ns1:image> 
    </ns1:download> 
XML; 
$f = file_get_contents("test.jpg");           

$responseMessage = new WSMessage($responsePayloadString, 
     array("attachments" => array("myid1" => $f))); 
return $responseMessage;  
} 

$operations = array("download" => "sendAttachment"); 

$service = new WSService(array("operations" => $operations, "useMTOM" => TRUE)); 

$service->reply(); 

?> 

客户:

<?php 

$requestPayloadString = '<download></download>'; 

try { 

$client = new WSClient(
    array("to" => "http://SPLINTER/MTOM/service.php", 
      "useMTOM" => TRUE, 
      "responseXOP" => TRUE)); 

$requestMessage = new WSMessage($requestPayloadString);      
$responseMessage = $client->request($requestMessage); 

printf("Response = %s \n", $responseMessage->str); 

$cid2stringMap = $responseMessage->attachments; 
$cid2contentMap = $responseMessage->cid2contentType; 
$imageName; 
if($cid2stringMap && $cid2contentMap){ 
    foreach($cid2stringMap as $i=>$value){ 
     $f = $cid2stringMap[$i]; 
     $contentType = $cid2contentMap[$i]; 
     if(strcmp($contentType,"image/jpeg") ==0){ 
      $imageName = $i."."."jpg"; 
      if(stristr(PHP_OS, 'WIN')) { 
       file_put_contents($imageName, $f); 
      }else{ 
       file_put_contents("/tmp/".$imageName, $f); 
      } 
     } 
    } 
}else{ 
    printf("attachments not received "); 
} 

} catch (Exception $e) { 

if ($e instanceof WSFault) { 
    printf("Soap Fault: %s\n", $e->Reason); 
} else { 
    printf("Message = %s\n",$e->getMessage()); 
} 
} 
?> 

相反的,我想开一个 “保存对话框中的” 打开或保存文件之间进行选择。当我搜索解决方案时,我阅读了有关设置heders的信息,例如:

header('Content-type: application/octet-stream'); 
header('Content-disposition: attachment; filename="test.jpg"'); 

但它没有正常工作。 “保存对话框”强制关闭,但当图像无法打开时说该文件是空的。

其实我不明白很好这MTOM附件的事情是如何工作的。在客户端代码中,我认为$ f是一个字符串,当我做printf($ f)时,它打印0(零),那么如何将该字符串保存为图像?在advandce感谢!

回答

0

如果你想使用头,你要输出的文件内容,而不是保存在某个地方吧。

header('Content-type: application/octet-stream'); 
header('Content-disposition: attachment; filename="test.jpg"'); 

// Output file. This must be the ONLY output of the whole script 
echo $rawFileContents; 

的基础是,此刻的你有你的整个文件内容的可变装载(这似乎是$f在你的代码),你输出它,而不是它在文件中写的(我想你现在正在做)。所以,我给你的三行代码应该取代file_put_contents()调用。

相反,如果你想将文件保存在您的/tmp文件夹,OK,这样做,但后来改用

header('Location: /tmp/' . $imageName); 

这样,你直接将用户重定向浏览器保存的文件,并让用户做他们想要的东西。

+0

凡在我的代码究竟应该我把你提供的标题代码?我是否应该使用任何其他标题或只是这个?据我了解,我必须删除file_put_contents($ imageName,$ f);对?另外,非常快速的答复,谢谢你。 – petter386

+0

我承认你的代码很杂乱,我没有看完。基础知识是,此刻的你有你的整个文件内容的可变装载(这似乎是'$ F'),你可以把它放在一个文件(因为我认为你正在做的),或者输出。所以,我给你的三行代码应该代表'file_put_contents()'调用。 –

+0

似乎该文件是从foreach循环的第一行中的附件中提取的。就像你说的 - $ f。我用你的3行代替了file_put_contents(),但没有区别。你是对的,我想通过“保存对话框”将这个图像从$ f保存到文件中。我是否必须改变其他任何东西来完成这项工作,因为它仍然说在试图打开图像时该文件是空的。 – petter386