How do I download JavaScript string as a file

前端 未结 8 835
太阳男子
太阳男子 2021-01-13 18:52

Application requests KML data through AJAX from server. This data is stored in javascript variables, and displayed in Google Earth plugin.

In javascript, how do I

8条回答
  •  猫巷女王i
    2021-01-13 19:33

    This will work! It works for me.

    enter link description here

    `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);
    }
    
    // Start file download.
    download('hello.txt','This is the content of my file ');
    
    `

提交回复
热议问题