![](/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.