我从REST API获取字节数组。然后我从这个字节数组创建一个本地文件。我可以将此文件与ZF2一起发送到浏览器。
ZF2控制器操作中的代码:
file_put_contents($fullpath, $rawPdf);
$headers = new \Zend\Http\Headers();
$contentDisposition = ($view == 'inline') ? 'inline': 'attachment';
$headers->addHeaders(array(
'Content-Disposition' => $contentDisposition . '; filename="' . basename($fullpath) . '"',
'Content-Type' => 'application/pdf',
'Content-Length' => filesize($fullpath),
'Expires' => '@0',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'public'
));
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($fullpath, 'r'));
$response->setStreamName(basename($fullpath));
$response->setStatusCode(200);
$response->setHeaders($headers);
return $response;
但我会直接将字节数组发送到浏览器而不创建本地文件。
如何使用Byte Array设置流($ response-> setStream())?如何使用字节数组创建资源,但不创建本地文件?
使用普通的旧PHP,我可以这样做:
$rawPdf = '';
array_walk($byteArray, function($value) use (&$rawPdf) {
$rawPdf .= chr($value);
});
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="Bericht.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $rawPdf;
答案 0 :(得分:1)
这是我用于将某些数据导出CSV文件并自动下载的代码的简化示例。
您可以根据自己的需要进行调整
<select class='species_select' id='species'+siteNumber+'1'>
<option disabled selected>Select species</option>
<option value='unknown'>unknown</option>
</select>
<select class='species_select' id='species'+siteNumber+'2'>
<option disabled selected>Select species</option>
<option value='unknown'>unknown</option>
</select>
<script>
$(document).on("click", "#next_to_summary", function(){
var proceed = true;
$(".species_select").each(function() {
if (this.value === null){
alert("Please select the species");
proceed = false;
};
});
</script>