这是我尝试通过ajax打开pdf,但显然有些不正确。
(代码片断)
function show_invoice() {
$data = $this->input->get('info', TRUE);
$user_id = $data['user_id'];
if ($user_id != strval(intval($user_id))) {
throw new exception('no id for user is set for creating pdf');
}
//Load pdf into browser
$filename_pdf = 'pdf/invoice' . $user_id . '.pdf';
header("Content-Length: " . filesize ( $filename_pdf ) );
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".basename($filename_pdf));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
//Must use readfile because the file isn't
//accesible through the browser through for example
//http://domain/pdf/invoice_21.pdf
readfile($filename_pdf);
}
js(code-snippet)
var info = {};
info.user_id = user_id;
//Send get request to show pdf
var showInvoice = $.ajax({
type: 'GET',
data: {info: info},
url: CI.base_url + 'member/show_invoice', //CI.base_url is a CodeIgniter constant
dataType: "json" //WHAT datatype to use here?
});
showInvoice.done(function(data) {
//do something
});
我也试过echo json_encode(readfile($filename_pdf));
的东西(但我暂时不相信)
更新
我正在使用AJAX,因为我无法像<a href="domain/pdf/invoice_21.pdf">Show invoice</a>
这样做,因为该文件无法直接在浏览器中访问。