Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get multiple file input from a single input

Using browser firefox and chrome

I have an input file element.

<input type='file' id='tempFileInput' multiple></input>

Let say I have selected three files for the above file input box ('tempFileInput');

OnChange I want to separate three files into three new file input boxes for each file. i.e

<input type='file' id='inputFile_0'></input> 
<input type='file' id='inputFile_1'></input> 
<input type='file' id='inputFile_2'></input>

I'm struggling to achieve this. Any help?

//I have written a small JavaScript snippet towards what I wana achieve.

var index = 0;
function multipleInputBoxes(tempFileInput){
   var divForm = document.getElementById('divForm');
   var numOfFiles = tempFileInput.files.length;

   for(var i=0; i<numOfFiles; i++){
      var newUploader = document.createElement('input');
      newUploader.type='file';
      newUploader.id = 'inputFile_' + index;

      var file = tempFileInput.files[i];
      ***newUploader.files[0] = file;***
      //above line does not work, as by default due to security reasons input type='file' is read only, and non editable.

      divForm.appendChild(newUploader);
      index++;
   }
}
like image 902
user1450322 Avatar asked Jun 12 '12 05:06

user1450322


1 Answers

Found scattered code in other posts and assembled a working example, that breaks down input file 'multiple' files into individual files and allows you to remove them or add new ones without losing the previously selected ones:

JS code

// Requires jQuery

function addFileToNewInput(file, newInput) {
  if (!newInput) { return }

  var dataTransfer = new DataTransfer()
  dataTransfer.items.add(file)
  newInput.files = dataTransfer.files
}

function addFileNameToPreview(file, preview) {
  if (!preview) { return }

  preview.innerText = file.name
}

function breakIntoSeparateFiles(input, targetSelector, templateSelector) {
  var $input = $(input)
  var templateHtml = $(templateSelector).html()

  if (!input.files) { return }

  for(var file of input.files) {
    var $newFile = $(templateHtml).appendTo(targetSelector)
    addFileToNewInput(file, $newFile.find("input")[0])
    addFileNameToPreview(file, $newFile.find(".file-name")[0])
  }

  $input.val([])
}

HTML

<!-- Requires bootstrap css for style -->

<form class="m-5">
  <div id="file-list"></div>

  <label for="upload_input" class="btn btn-warning">Upload</label>
  <input
    id="upload_input"
    type="file"
    name="post[attachments][]"
    class="d-none"
    multiple="multiple"
    onchange="window.breakIntoSeparateFiles(this, '#file-list', '#file-preview')"
  />

  <template id="file-preview">
    <div class="file-preview mb-2">
      <span class="file-name"></span>
      <button
        class="btn btn-sm btn-danger ml-2"
        onclick="$(this).closest('.file-preview').remove()"
       >&times;</button>

      <input class="d-none" multiple="multiple" type="file" name="post[attachments][]">
    </div>
  </template>
</form>

I wrote an article on this which has more examples: https://medium.com/@goncalvesjoao/break-input-file-into-individual-files-and-preview-them-29fdbab925b2

like image 101
Joao Goncalves Avatar answered Sep 28 '22 00:09

Joao Goncalves