【发布时间】:2018-01-03 09:26:29
【问题描述】:
点击链接时,我正在尝试下载 pdf。
我调用了链接的 onlick 函数,在 Codeigniter 中我编写了一个下载 PDF 文件的函数,但是在运行 URL 时,文件被下载,但是当使用点击链接触发时,它不起作用。
控制器:
function downloadpdf($pid)
{ set_time_limit(0);
$url="http://www.malayatourism.com/uploads/images/packages/pdf/$pid.pdf";
$file = basename($url);
$fp = fopen($file, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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);
echo "success";
}
我的代码 ajax:
function DownLoadPdf(id)
{
url = "<?php echo base_url();?>admin/packages/downloadpdf/"+id;
$.ajax({
type:'POST',
url: url,
data : { pid : id},
success : function(response) {
console.log(response);
},
error : function(response) {
console.log("error");
}
});
}
查看:
echo '<a href="javascript:void()" title="Hapus" onclick="DownLoadPdf(' . "'" . $cdata[0]->package_id . "'" . ')"> Download Attachment</a>';
【问题讨论】:
-
我猜你在这里为文件创建 URL 时遇到了问题 $url="malayatourism.com/uploads/images/packages/pdf/$pid.pdf"; $file = basename($url);你能告诉我这里 $file 的输出吗?
-
查看这篇文章。它可能会帮助你。 stackoverflow.com/questions/4545311/…
标签: javascript php jquery file download