php - Open PDF in new tab using PHP
问题描述
I am simply trying to open a .pdf in another tab.
I start by using jQuery to post a couple of variables:
$('#resultsTable').on('click', 'tr > td > .view-pdf', function()
{
var $dataTable = $('#resultsTable').DataTable();
var tr = $(this).closest('tr');
var rowBookingNum = $dataTable.row(tr).data().JOB_REFERENCE;
var rowPartnerNum = $dataTable.row(tr).data().SHIPPER_CODE;
$.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum });
});
The PHP script looks like this:
<?php
if(isset($_POST["bookingnum"]))
{
$bookingnum = $_POST["bookingnum"];
$partnernum = $_POST["partnernum"];
$dir = "D:/CargoDocsPDFs/" . $partnernum;
$file = $dir . "/" . $bookingnum . '.pdf';
if (file_exists($file))
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
?>
Using all of the above, I can get the .pdf to open in the same window. This causes the user to have to click the BACK button to go back to the previous screen.
I need the .pdf to open in a new tab. How can I adjust my code to make this happen?
解决方案
You need to specify a target in $.redirect(url, [values, [method, [target, [traditional, [redirectTop]]]]])
See usage: http://github.com/mgalante/jquery.redirect
Desired usage:
$.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum }, 'POST', '_blank');
推荐阅读
- rust - 从文件反序列化 toml 时出现无用的错误
- arrays - 超出缓冲区末尾的读取如何在 C 中工作?
- php - 如果时间结束,如何在php中显示小时和分钟
- python - Python Numpy 与 Datafame np.select 与数组值
- python - 运行我的代码时无法获得 predict_proba
- python - 寻找处理 ML 分类数据的最佳方法
- c# - C# 中带有城市名称的时间偏移格式
- google-sheets - 谷歌表格:计算一串单词中的特定字符
- c++ - 具有 2 个键的 C++ 映射,因此可以使用任何 1 个键来获取值
- javascript - Vuex - 在使用 mapState 数据之前等待 api 调用和初始存储?