-2
我收到一個來自角度$ http.get請求的二進制pdf文件。我試圖打開一個對話框來提示用戶使用readfile()下載文件,但沒有顯示對話框。此外,有沒有辦法只使用二進制數據,而不將其寫入文件系統以與readfile()一起使用,以便提示用戶下載文件?php提示用戶下載pdf
return $http({
method: 'get',
url: env.baseUri + '/1.0/releases/report/file?regions=' + regionIds
})
.then(
function (response) {
getPdfFactory.getPdf(response.data)
.then(function(pdf){
// replace with php
//window.open("data:application/pdf;base64, " + pdf.data);
...
<?php
if(isset($_POST)) {
$postdata = file_get_contents("php://input");
$binary = json_decode($postdata);
// $base64pdf = base64_encode($binary);
file_put_contents("../../../src/uploads/report.pdf", $binary);
// echo $base64pdf;
$file = "../../../src/uploads/report.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>