How can I convert multiple files at once in JavaScript (local files)?

心不动则不痛 提交于 2020-01-05 05:45:32

问题


I have created a converter that takes in a SVG file and gives me a text file as output, with some modifications. How can I modify this to apply to a batch of files?

I have tried to use multiple="multiple", but when I choose input, it doesn't give me an option to select multiple files.

document.getElementById("fileReader").addEventListener('change', function() {
  var filePath = document.getElementById("fileReader").value;
  var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("\."));
  var fr = new FileReader();
  fr.onload = function() {;
    var d = new DOMParser().parseFromString(this.result.toString(), "image/svg+xml");
    var textOut = "";
    //various conversion operations.
    function download(filename, text, type = "text/plain") {
      // Create an invisible A element
      const a = document.createElement("a");
      a.style.display = "none";
      document.body.appendChild(a);

      // Set the HREF to a Blob representation of the data to be downloaded
      a.href = window.URL.createObjectURL(
        new Blob([text], {
          type
        })
      );

      // Use download attribute to set set desired file name
      a.setAttribute("download", filename);

      // Trigger the download by simulating click
      a.click();

      // Cleanup
      window.URL.revokeObjectURL(a.href);
      document.body.removeChild(a);
    }
    // Starting file download.
    document.getElementById("button").onclick = function() {
      download(fileName, textOut);
    };
  }
  fr.readAsText(this.files[0]);
})
<input type="file" id="fileReader" multiple="multiple" />

<input id="button" type="button"/>

What changes would allow me to do this to multiple files at once? I want each file selected to be converted and downloaded as a seperate file. Is there any way to do this to an entire folder?

来源:https://stackoverflow.com/questions/57817483/how-can-i-convert-multiple-files-at-once-in-javascript-local-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!