DEV Community

Basha
Basha

Posted on

How to download a text using js

Hello:)
You can easily download a custom text using js.

function Download(){
    var name = "file name"

    const content = "text";
    const element = document.createElement('a');
    const blob= new Blob([content], {
        type:'plain/text'
    });
    const fileurl = URL.createObjectURL(blob);
    element.setAttribute('href', fileurl);
    element.setAttribute('download', name+".html");
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();

    document.body.removeChild(element);
}
Enter fullscreen mode Exit fullscreen mode

This function will download a file as a text file

Top comments (0)