【发布时间】:2022-12-02 19:13:34
【问题描述】:
I\'m trying to save some string content into a file from JavaScript. Below is my code and I\'m getting issues because of new lines in the String content.
How to save a file with preserving new lines?
var text = \"Hello \\n World!\"
var file = new Blob([text], {{type:\'text/plain\'}});
var anchor = document.createElement(\"a\");
anchor.href = URL.createObjectURL(file);
anchor.download = \"file.log\";
anchor.click();
标签: javascript file
\\nyou\'ve shown in your code isn\'t really a\\n(an escape sequence, backslash followed bynwhich produces a newline in the resulting string), but an actual newline in the string literal, which is indeed a syntax error. But it\'s really not clear how you\'re getting from A (the code you\'ve posted) to B (the error in the screenshot). Could you clarify?var file = new Blob([text], {{type:\'text/plain\'}});are an error, it should be justvar file = new Blob([text], {type:\'text/plain\'});.