Home  >  Q&A  >  body text

How to download an excel file with javascript?

 <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?

滿天的星座滿天的星座2674 days ago865

reply all(6)I'll reply

  • 某草草

    某草草2017-06-23 09:16:30

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

    reply
    0
  • 欧阳克

    欧阳克2017-06-23 09:16:30

    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>

    reply
    0
  • PHP中文网

    PHP中文网2017-06-23 09:16:30

    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...

    reply
    0
  • 高洛峰

    高洛峰2017-06-23 09:16:30

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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-23 09:16:30

    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.

    reply
    0
  • 迷茫

    迷茫2017-06-23 09:16:30

    <a href="xx.xlsx"></a>

    reply
    0
  • Cancelreply