How to create .txt file using JavaScript / HTML5?

后端 未结 2 1062
抹茶落季
抹茶落季 2020-12-14 22:47

I am new to javascript . all codes available on the internet related to create text file using javascript is not working in my laptop. can anybody give me idea or with possi

2条回答
  •  独厮守ぢ
    2020-12-14 23:28

    A very fast and easy solution is to use FileSaver.js :
    https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js

    Then it takes only 2 lines of code to download a txt file :

    var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
    
    saveAs(blob, "hello world.txt");
    


    This code example will display a dialog box to download a file named "hello world.txt" containing the text "Hello, world!". Just replace this by the file name and the text content of your choice !

提交回复
热议问题