How to trigger a file download when clicking an HTML button or JavaScript

前端 未结 21 2782
终归单人心
终归单人心 2020-11-22 05:27

This is crazy but I don\'t know how to do this, and because of how common the words are, it\'s hard to find what I need on search engines. I\'m thinking this should be an ea

21条回答
  •  故里飘歌
    2020-11-22 06:29

    This is what finally worked for me since the file to be downloaded was determined when the page is loaded.

    JS to update the form's action attribute:

    function setFormAction() {
        document.getElementById("myDownloadButtonForm").action = //some code to get the filename;
    }
    

    Calling JS to update the form's action attribute:

    
    

    Form tag with the submit button:

    Click to open document:

    The following did NOT work:

提交回复
热议问题