Client side file creation and download

前端 未结 5 969
南方客
南方客 2021-01-05 15:04

We are using fusioncharts and it has the ability using javascript on the client side to export csv data, we want to be able to take that data and create a file on the fly in

5条回答
  •  借酒劲吻你
    2021-01-05 15:26

    Since Marc's answer was (stupidly) converted to a comment, and none of the other answers actually answer the question, here's the answer:

    Click me to DL something
    
    

    This was derived from the answer Marc referenced, which is useful in situations where you're not specifically clicking on a link tag: https://stackoverflow.com/a/3665147/279255

    // must be called in a click handler or some other user action
    function download(filename, text) {
      var element = document.createElement('a');
      element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
      element.setAttribute('download', filename);
    
      element.style.display = 'none';
      document.body.appendChild(element);
    
      element.click();
    
      document.body.removeChild(element);
    }
    

提交回复
热议问题