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

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

    I think this is the solution you were looking for

    
    

    I hade a case where my Javascript generated a CSV file. Since there is no remote URL to download it I use the following implementation.

    downloadCSV: function(data){
        var MIME_TYPE = "text/csv";
    
        var blob = new Blob([data], {type: MIME_TYPE});
        window.location.href = window.URL.createObjectURL(blob);
    }
    

提交回复
热议问题