简体   繁体   中英

how to customize file content using Angular js

I am generating a txt file using angularjs. I have some problem with data inside the file, this is a small snippet of my code :

var fileContent =  ["text line 1","text line 2","text line 3","text line 4","text line 5","text line 6"];
var blob = new Blob([ fileContent ], { type : 'text/plain' });
$scope.url = (window.URL || window.webkitURL).createObjectURL( blob ); 

html :

<a download="file.txt" ng-href="{{ url }}">download</a>

the result in the file is :

text line 1,text line 2,text line 3,text line 4,text line 5,text line 6

I want to show the result as follows :

text line 1
text line 2
text line 3
text line 4
text line 5
text line 6

I want to show each element in the array on one line and without ","

Escape a new line. "text line 1" + \\n

Do this:

var fileStr = fileContent.join("\n");

You can use replace function as follows:

var resultFile = "text line 1,text line 2,text line 3,text line 4,text line 5,text line 6";
var newOutput = x.replace(/,/g, "\n");

The newOutput value should be:

text line 1
text line 2
text line 3
text line 4
text line 5
text line 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM