![](/img/trans.png)
[英]Why is the download interrupted when i call chrome.downloads.download method with enabled "Ask where to save each file before downloading" option
[英]Creating a multi-line file and downloading with Chrome.downloads.download
我正在生成一些内容客户端,并且我想使用chrome.downloads.download下载此生成的内容。 请注意,下载工作正常,但是以某种方式下载的文件不包含新行(我使用
lineContent += '\r\n';
这行不通。 我试过了
'\r' or '\n'
同样,但是没有运气。 在生成的文档中,除了换行符以外的所有内容都是正确的。 有什么想法为什么它可能无法显示新行?
我已经尝试过不同的编辑器,所以我怀疑编辑器是为什么将所有内容都显示在一行中的原因。
// when I'm debugging the fileData is showing as multi-lined, but after
// download, all of the content is a single line.
chrome.downloads.download({
url: "data:text/plain," + fileData,
filename: 'file.txt',
conflictAction: "prompt",
saveAs: true,
}, function(downloadId) {
console.log("Downloaded item with ID", downloadId);
});
下载时使用%0A( \\n
的url编码实体)保存它,或者直接使用它,或者将您的文本传递给encodeURIComponent()
以对相应的字符进行url编码
function download1(){ a = document.createElement("a") a.href = "data:text/plain,Stackoverflow%0ANewline"; a.download = "test.txt"; a.click(); } function download2(){ a = document.createElement("a") a.href = "data:text/plain,"+encodeURIComponent("Stackoverflow\\nNewline"); a.download = "test.txt"; a.click(); }
<button onclick="download1()">Download 1</button> <button onclick="download2()">Download 2</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.