简体   繁体   English

从 Web 服务接收的文件的 PHP“保存对话框”

[英]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.

相关问题 php从ftp服务器获取文件并显示保存文件对话框 - php get file from ftp server and display save file dialog 将文件从PHP发送到Web服务 - Sending file from Php to web service PHP保存文件而不另存为对话框 - PHP save file without save as dialog 解密从PHP服务收到的AES缓冲区 - Decrypt AES buffer received from PHP service 如何将图像保存到从iOS应用接收的文件中,该应用使用PHP编码为Hex? - How to save an image to a file received from an iOS App with PHP encoded as Hex? 如何在PHP中将文件从URL保存到Sourceforge Web服务器? - How to save file from URL to Sourceforge web server in PHP? 如何在jquery对话框表中正确显示从php接收的数据 - How to display received data from php properly in jquery dialog table 将收到的数据保存到Woocommerce 3中的外部交付服务中 - Save received data to the order from an external delivery service in Woocommerce 3 通过保存对话框形式将由 php 脚本生成的文件保存到目录中 - save a file generated by a php script in to a directory by save dialog form 如何使用php中的“另存为”对话框将特定CSV文件从服务器下载到客户端? - How to download a specific CSV file from the server to the client using Save As dialog box in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM