0

I manage to get this work but how to actually make it this happen when the variable actually is an array of multiple objects which you can write to each lines with blob?

var line1 = "firstline";
var line2 = "secondline";
var blob = new Blob([line1 + "\r\n", line2], {type: "application/txt"});

var url  = URL.createObjectURL(blob);

var a = document.createElement('a');
a.download    = "test.txt";
a.href        = url;
a.textContent = "Download test.txt";

document.getElementById('test').appendChild(a);
3
  • "the variable actually is an array" - what is "the variable" referencing here? There are a lot of variables there Commented Nov 9, 2014 at 13:35
  • sorry, I've find the solution already thanks ^^ Commented Nov 9, 2014 at 20:25
  • In that case, it might be a good idea to provided an answer to your own question (you can do that). Maybe someday someone is going to have a similar problem and it'll be helpful for him to see how you handled it Commented Nov 10, 2014 at 10:20

1 Answer 1

1

well sorry, this is the answer, which is pretty silly for me. When the variable is an array of objects simply use join() to add lines breaks. So the output will nicely become text file with all array objects appear on new lines.

var arr = ['firstline', 'secondline', 'thirdline'];
var lineConcat = arr.join("\r\n");
var blob = new Blob([lineConcat], {type: "application/txt"});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.