我有一本字典,我想用多行打印到一個txt文件。如何將多行字符串打印到blob中?
要做到這一點,我:
1)通過JSON.stringify的字典轉換爲字符串(字典)
2)輸入字符串的Blob,並將其保存
我有麻煩是輸出是字典的真難看1行的結果,這樣的:
{"key1":"value1","key2":"value2","key3":"value3",etc}
但我想它是這樣的:
key1:value1
key2:value2
key3:value3
etc.
我該如何做到這一點?
result = JSON.stringify(dict);
// Create blob instance and input the string
var blob1 = new Blob([result], { type: "text/plain" });
url = window.URL.createObjectURL(blob1);
// Create link to download the blob as txt file
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.setAttribute("download","My Open Tabs");
a.click();
window.URL.revokeObjectURL(url);
使用JSON.stringify =>'JSON.stringify(dict,null,'');'的第三個參數。見這裏https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – Fefux