String Array to Data URL and Back

by skibulk

JavaScript

console.clear();

function arrayToDataURL(arr) {
  return "data:text/plain;charset=UTF-8," + encodeURIComponent(arr.join("\r\n"));
}

var f = arrayToDataURL(["Hello", "World?"]);
console.log(f);

function srcToFile(src, fileName, mimeType) {
  return (fetch(src)
    .then(function(res) {
      return res.arrayBuffer();
    })
    .then(function(buf) {
      return new File([buf], fileName, {
        type: mimeType
      });
    })
  );
}

srcToFile(f, 'hello.txt', 'text/plain').then(function(file) {
  console.log(file);
});