如何在下载中创建多个文件然后将它们附加到JS中

时间:2016-03-16 20:22:45

标签: javascript

我有一个输入文件并操作它的按钮我想首先创建他们追加它们的新文件所以我只是读取较小的数据片段,以便在我试图打开一个非常大的时候不会使浏览器过载文件。

    function SplitFile(){
      num_files = document.getElementById("piece-up").value;

      var control = document.getElementById("your-files");
      files = control.files;
      len = files.length;

      var test = document.getElementById("test");
      test.value = '';
      var i = 0;
      while (i < len){
        test.value += i + "\n";
        test.value += "Filename: " + files[i].name;
        test.value += "\n" + "Type: " + files[i].type;
        test.value += "\n" + "Size: " + files[i].size + " bytes";

        var file_length = files[i].size;
        var slice_size = 100;
        var remainder = file_length % slice_size;
        var num_large = file_length - remainder;
        var num_loops = num_large / slice_size;
        var slice_0 = 0;
        var slice_1 = slice_size;
        var q = 0;
        test.value += "\n" + "Num Loops: " + num_loops;

        while(q< num_loops){
          var myReader = new FileReader();
          myReader.readAsText(files[i].slice(slice_0, slice_1));
          myReader.onload = function(event) {
            test.value += "\n" + "Loop: " + q;
            var contents = event.target.result;
            test.value += "\n" + contents;
            spliceContents(num_files, contents, slice_size);
          };
          slice_0 = slice_0 + slice_size;
          slice_1 = slice_1 + slice_size;
          q++;
        }
        var myReader = new FileReader();
        slice_0 = slice_1 - slice_size;
        slice_1 = slice_1 + remainder - slice_size;
        myReader.readAsText(files[i].slice(slice_0, slice_1));
        myReader.onload = function(event) {
          test.value += "\n" + "Loop: " + q;
          var contents = event.target.result;
          test.value += "\n" + contents;
        };
        i++;
      }

    }

function spliceContents(num_files, contents, slice_size) {
  var i = 0;
  while(i<slice_size){
    var mod = i % num_files;
    appendFile(mod, contents[i]);
    i++;
  }
}

function appendFile(mod, contents){

}

0 个答案:

没有答案