How to read and write into file using JavaScript?

后端 未结 17 2491
孤街浪徒
孤街浪徒 2020-11-22 02:00

Can anybody give some sample code to read and write a file using JavaScript?

17条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 02:45

    Writing this answer for people who wants to get a file to download with specific content from javascript. I was struggling with the same thing.

    const data = {name: 'Ronn', age: 27};              //sample json
    const a = document.createElement('a');
    const blob = new Blob([JSON.stringify(data)]);
    a.href = URL.createObjectURL(blob);
    a.download = 'sample-profile';                     //filename to download
    a.click();
    

    Check Blob documentation here - Blob MDN

提交回复
热议问题