Create a download link for text content.

HTML

<button onclick="downloadText('說明.txt', '這是一段要下載的文字內容,測試許功蓋')">
    下載文字
</button>

JavaScript

function downloadText(filename, text) {
    const blob = new Blob([text], {
        type: 'text/plain;charset=utf-8'
    });

    const url = URL.createObjectURL(blob);

    const a = document.createElement('a');
    a.href = url;
    a.download = filename;

    document.body.appendChild(a);
    a.click();

    document.body.removeChild(a);
    URL.revokeObjectURL(url);
}