[英]PHP “save dialog” for file received from web service
I'm using WSO2 WS Framework and I managed to run example in which web service returns an image as a MTOM attachment which is then saved by the client using file_put_contents(...) command.我正在使用WSO2 WS 框架,并且设法运行示例,其中 Web 服务将图像作为 MTOM 附件返回,然后客户端使用 file_put_contents(...) 命令保存该图像。
Service:服务:
<?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();
?>
Client:客户:
<?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());
}
}
?>
Instead of that I would like to open a "Save dialog" to choose between opening or saving the file.相反,我想打开一个“保存对话框”以在打开或保存文件之间进行选择。 When searching for solution I read about setting heders like:
在寻找解决方案时,我阅读了有关设置 heders 的内容,例如:
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="test.jpg"');
But it didn't work well.但效果并不好。 "Save dialog" poped up, but when image couldn't be opened saying that file is empty.
弹出“保存对话框”,但无法打开图像时说该文件为空。
Actually I don't understand very good how this MTOM attachments thing is working.其实我不太明白这个 MTOM 附件是如何工作的。 In client code, I think $f is a string and when I do printf($f) it prints 0(zero) so how can I save this string as an image?
在客户端代码中,我认为 $f 是一个字符串,当我执行 printf($f) 时,它会打印 0(zero) 那么如何将此字符串保存为图像?
If you want to use that headers, you have to output the file content, not save it somewhere.如果要使用该标题,则必须输出文件内容,而不是将其保存在某处。
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;
The basics is that at the moment you have your whole file content loaded in a variable (and it seems is $f
in your code), you output it instead of write it in a file (as I think you're doing now).基本原理是,目前您将整个文件内容加载到一个变量中(并且在您的代码中似乎是
$f
),您将其输出而不是将其写入文件(正如我认为您现在正在做的那样)。 So, the three lines of code I gave you should replace the file_put_contents()
calls.所以,我给你的三行代码应该替换
file_put_contents()
调用。
Instead, if you want to save the file in your /tmp
folder, ok, do it, but then use instead相反,如果你想将文件保存在你的
/tmp
文件夹中,好的,这样做,然后使用
header('Location: /tmp/' . $imageName);
This way you redirect the user browser directly to the saved file, and let users do what they want with it.通过这种方式,您可以将用户浏览器直接重定向到保存的文件,并让用户使用它做他们想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.