How to download an excel file with javascript?
滿天的星座
滿天的星座 2017-06-23 09:14:30
0
6
898
 <form method="get" action="/app/PoiTemplate/NFC.xlsx">
                        <button type="submit">Download!</button>
                    </form>
window.open("/app/PoiTemplate/NFC.xlsx") 

Both of the above methods have opened the file. Is there any other downloading method?

滿天的星座
滿天的星座

reply all(6)
某草草
<a href="nfc.xlsx" download="nfc.xlsx">下载</a>

The following is the header of php, try it yourself

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 " ); 
学霸

Although IE does not recognize the download attribute, it is still a link under IE, so we can add a target="black" attribute to open a new window and implement downloading, so it can be implemented as follows:

<a href="nfc.xlsx" download="nfc.xlsx" target="_blank">下载</a>
洪涛

Your problem is not with the front end. Another way of thinking is to download through the back end, initiate a get request at the front end, and set the return header Content-Disposition: Attachment; filename="file name" in the back end in the form of a stream. , the browser will naturally pop up the file selection box. Here is a java example https://github.com/treeandgra...
Second idea:
/q/10...

小葫芦

Based on my experience, your problem should be caused by the browser, try another browser

phpcn_u1582

The characteristic of javascript lies in the operation and reference of DOM and BOM. File download is a feature of Meta. JavaScript has no permission to operate. Just let the back-end developer set the output Head Meta.

迷茫
<a href="xx.xlsx"></a>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template