Creating Javascript Blob() with data from HTML Element. Then downloading it as a text file

后端 未结 3 2165
醉酒成梦
醉酒成梦 2020-12-20 16:11

I\'m using an HTML5 site to create a log per-say within a textarea element. I need to pull the data from that area with the click of a button, and download it to my computer

3条回答
  •  时光取名叫无心
    2020-12-20 17:02

    I used this approach that doesn't involve creating an element and revokes the textFile after the browser showed the text file

    var text = 'hello blob';
    var blob = new Blob([text], { type: 'text/plain' });
    let textFile = window.URL.createObjectURL(blob);
    let window2 = window.open(textFile, 'log.' + new Date() + '.txt');
    window2.onload = e => window.URL.revokeObjectURL(textFile);
    

提交回复
热议问题