保留数据中的换行符:text URI [英] Preserving newline characters in data:text URI
问题描述
在我的扩展中有一个按钮触发以下代码:
chrome.tabs.create({url:' data:text; base64,'+ btoa(data),active:false});
如我所料,这会触发我的字符串(数据)下载。不幸的是,它似乎在剥离换行符。
我尝试了其他编码方法,包括utf-8和 encodeUri() code>函数。我也尝试将mimetype切换为
data:text / plain
,但只是在新选项卡中打开(使用正确的换行符)而不是下载。
有没有办法对我的文本进行编码,以便保留换行符?如果没有,有什么不同的方法可以在浏览器中触发文件下载?
edit
我发现换行符出现在一些文本编辑器中。以前,我使用的是notepad,它不能识别chrome中的换行符,但我的其他文本编辑器(notepad ++)似乎确实可以识别它们。解决方案
div>我推荐的解决方案是不使用记事本,因为它不识别非Windows行格式。如果您仍然希望能够在输出中使用记事本,用回车+换行对( 0x0D 0x0A
0x0A ) > 。
$ b
chrome.tabs.create({
url:'data: text; base64,'+ btoa(data.replace(/ \ n / g,'\r\\\
')),
active:false
});
I have a button in my extension that triggers the following code:
chrome.tabs.create({url: 'data:text;base64,'+btoa(data), active:false});
This triggers a download of my string (data), as I expected. Unfortunately, it seems to be stripping out newline characters.
I have tried other encoding methods, including utf-8 and the encodeUri()
function. I also tried switching the mimetype to data:text/plain
, but that simply opens in a new tab (with the correct newline characters) instead of downloading.
Is there a way to encode my text so that newline characters are preserved? If not, is there a different method for triggering file downloads in the browser?
edit
I have discovered that the newlines do appear in some text editors. Previously, I was using notepad, which did not recognize the newline characters from chrome, but my other text editor (notepad++) does seem to recognize them
My recommended solution is to not use Notepad, because it does not recognize non-Windows line formats. If you still want to be able to use Notepad with your output, replace all line feeds (0x0A
) with carriage return+line feed pairs (0x0D 0x0A
).
chrome.tabs.create({
url: 'data:text;base64,' + btoa(data.replace(/\n/g, '\r\n')),
active: false
});
这篇关于保留数据中的换行符:text URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!