js 下載pdf檔案,而不是開啟預覽 方案
阿新 • • 發佈:2019-01-27
專案需求
常規加上download 屬性
<a href="https://testmv.xesimg.com/ExpCourse/image/2018/10/09/1539071754579183075997.pdf" download="filename" ></a>
這樣點選下載 會直接開啟pdf預覽,需要做的是直接彈出下載框進行下載
主要原因是因為檔案的請求頭部的問題,需要設定一下頭部
可以通過php後端做轉發設定一下
改後的前端程式碼
<a href="//i.xueersi.com/ExpCourse/dowloadFile?url=https://testmv.xesimg.com/ExpCourse/image/2018/10/09/1539071754579183075997.pdf& name=測試2.pdf" download></a>
php 介面程式碼
$url = $this->request->query['url'];
$name = $this->request->query['name'];
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $name . '"');
echo file_get_contents($url);