您的位置:首頁技術文章
文章詳情頁

javascript 如何下載一個excel文件 ?

瀏覽:100日期:2022-09-25 08:31:53

問題描述

<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.wxshucaidpc.com/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.wxshucaidpc.com/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.wxshucaidpc.com/wenda/xx.xlsx'></a>

標簽: excel
相關文章:
国产综合久久一区二区三区