簡體   English   中英

輸出文件中的新行字符

[英]New line character in output file

當我嘗試將具有多行的字符串寫入輸出文本文件時,不會保留換行符,並且所有內容都打印在一行中。

具體來說,我有一個按鈕,點擊一個監聽器並關聯此功能:

function (e) {
    this.downloadButton.setAttribute("download", "output.txt");
    var textToSend = string1+"\r\n"+string2+"\r\n"+string3;
    this.downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend);
}

文件正確下載,但string1,string2和string3在同一行。

有什么建議嗎?

我想您可能需要對數據進行編碼,您可以使用encodeURIComponent()進行編碼。

嘗試這個:

var textToSend = string1+"\r\n"+string2+"\r\n"+string3;
textToSend = encodeURIComponent(textToSend);
this.downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend)

使用encodeURIComponent() 見下面的工作示例。

 var downloadButton = document.getElementById('download'); var textToSend = encodeURIComponent("string1\\r\\nstring2\\r\\nstring3"); downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend); 
 <a id="download" download="output.txt">Download</a> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM