This is a minimum sample to save the text entered in textarea as a file.
- Node.js - 10.x
- Yarn - 1.12.x
- lite-server - 2.5.x
git clone git@github.com:yasu-s/blob-dl-js-sample.git
cd blob-dl-js-sample
yarn
yarn start
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Blob Sample</title>
<script>
function save() {
// get string from text area
const txt = document.getElementById('txt').value;
if (!txt) { return; }
// convert string to Blob
const blob = new Blob([txt], { type: 'text/plain' });
// a tag generation for download
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'sample.txt';
a.click();
};
</script>
</head>
<body>
<button onclick="save();">Save Text</button>
<br />
<textarea id="txt" cols="50" rows="5"></textarea>
</body>
</html>