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

前端 未结 21 2564
终归单人心
终归单人心 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:28

    There is a difference between loading a file and downloading a file. The following html code loads a file:

    loading on the same tab
    

    After clicking on this link, your current tab will be replaced with the pdf-file that can then be downloaded. On right-clicking on this link, you can choose the menu item save link as for downloading the file directly. If you wish to obtain a save as dialog on clicking on such a link, you might want to take the following code:

    save as...
    

    Your browser will download this file immediately if you choose to use a download directory in your options. Otherwise, your browser will be offering a save as-dialog.

    You can also choose a button for downloading:

    
    

    If you wish to load the link on a new tab, you take

    loading on a new tab
    

    A form element does not heed the directive ?download=1. It only heeds the directive target="_blank":

提交回复
热议问题