javascript 如何下載一個excel文件 ?
問題描述
<form method='get' action='/app/PoiTemplate/NFC.xlsx'><button type='submit'>Download!</button> </form>
window.open('/app/PoiTemplate/NFC.xlsx')
上面兩種方式都把文件給打開了, 還有其他下載方法嗎 ?
問題解答
回答1:<a href='http://www.leifengta.com.cn/wenda/nfc.xlsx' download='nfc.xlsx'>下載</a>
下面是php 的header,自己試試
header('Content-Type:?application/vnd.ms-excel;?charset=UTF-8'?);???header('Pragma:?public'?);???header('Expires:?0'?);???header('Cache-Control:?must-revalidate,?post-check=0,?pre-check=0'?);???header('Content-Type:?application/force-download');???header('Content-Type:?application/octet-stream'?);???header('Content-Type:?application/download'?);???header('Content-Disposition:?attachment;filename=11.xls?'?);???header('Content-Transfer-Encoding:?binary?'?);?回答2:
雖然IE不識別download屬性,但是IE下它任然是個鏈接,所以我們可以加一個 target='black' 屬性,來打開新窗口,實現下載,所以可以實現如下:
<a href='http://www.leifengta.com.cn/wenda/nfc.xlsx' download='nfc.xlsx' target='_blank'>下載</a>回答3:
你的問題并不在前端,換一種思路,通過后端下載,隨便在前端發起一個get請求,在后端設置返回頭 Content-Disposition: Attachment; filename='文件名',并以流的形式,瀏覽器自然后彈出文件選擇框。這兒有個java例子https://github.com/treeandgra...第二種思路:/q/10...
回答4:以我的經驗,你這個問題應該是出在瀏覽器上,換個瀏覽器試試
回答5:javascrit的特點在于DOM和BOM的操作和引用,文件下載屬于Meta的特性,javascript無權操作,讓后端開發人員設置輸出的Head Meta即可。
回答6:<a href='http://www.leifengta.com.cn/wenda/xx.xlsx'></a>
相關文章:
1. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. 我何時應該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)4. docker - 如何修改運行中容器的配置5. docker鏡像push報錯6. html5和Flash對抗是什么情況?7. javascript - 請指條明路,angular的$event,在select中卻是undefined?8. 利用IPMI遠程安裝centos報錯!9. javascript - 在 model里定義的 引用表模型時,model為undefined。10. phpstudy8.1沒集成mysql-front

網公網安備