function download(text, title) {
let a = document.createElement('a')
a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)
a.download = title
a.click()
}
title
不會像a標籤下載圖片視頻一般存在跨域問題,但 title
需要自行添加文件後綴 .txt
。
此外,如果需要在內容中輸入回車換行:
windows下的文本文件換行符: \r\n
,
linux/unix下的文本文件換行符: \r
,
Mac下的文本文件換行符: \n
。