Force download of 'data:text/plain' URL

后端 未结 5 2177
囚心锁ツ
囚心锁ツ 2020-12-01 16:31

I was wondering whether it is possible to force a browser (at least Chrome) to download a data:text/plain URL.

Chrome does download binary URLs (e.g.

5条回答
  •  情书的邮戳
    2020-12-01 17:12

    Here is a pure Javascript solution for creating a text blob and download as text file

    var fileContent = 'This is sample text file';
    var fileName = 'sampleFile.txt';
    
    const blob = new Blob([fileContent], { type: 'text/plain' });
    const a = document.createElement('a');
    a.setAttribute('download', fileName);
    a.setAttribute('href', window.URL.createObjectURL(blob));
    a.click();
    

提交回复
热议问题