user1982011
user1982011

Reputation: 307

Accessing Variable as a file instead

I want to refer to a variable that is a long string as a example.txt file I was wondering how I would go about doing this using Javascript. Since I cant save the variable as a .txt file, I was wondering if there was some way using javascript to refer to the variable which is a string as a .txt file.

Upvotes: 0

Views: 37

Answers (1)

PashaB
PashaB

Reputation: 109

Use Blob and objectUrl

//cross browser
window.URL = (
    window.URL ||
    window.webkitURL ||
    window.mozURL ||
    window.msURL
);

var example = new Blob([variable], {type: 'text/plain'}),
    url = window.URL.createObjectURL(example);

then basically you can refer to this variable using url or write this url to anchor and download it

document.body.innerHTML += '<a id="captislink" href="'+ url +'" download="example.txt">Save</a>';

Upvotes: 1

Related Questions