On my form, there is a button to select an image file. After pressing select You will be able to select additional image files. which every time a new image file is selected The input variable will also have a new value based on the last selected image. This causes me to store all image files with the variable in JS(imageMap). But when submitting the form, I can’t send these image files from the variable in JS(imageMap).
<form id="my_form" name="my_form" method="post" enctype="multipart/form-data" action="....">
....
<label for="img_picker">เลือกไฟล์ภาพ</label>
<input type="file" id="img_picker" class="hidden" multiple>
<div id="img_preview"></div>
<script>
var imageMap = {};
$('#img_picker').on('change', function () {
for (const file of this.files) {
const key = "id" + Math.random().toString(16).slice(2);
imageMap[key] = file;
const reader = new FileReader();
reader.onload = (e) => {
const img = document.createElement('img');
img.classList.add('img-thumbnail', 'mb-2');
img.style.width = '100px';
img.src = e.target.result;
const deleteButton = document.createElement('button');
deleteButton.classList.add('btn', 'btn-danger', 'btn-sm');
deleteButton.textContent = 'delete';
deleteButton.onclick = () => {
img.parentNode.removeChild(img);
deleteButton.parentNode.removeChild(deleteButton);
delete imageMap[key];
};
const div = document.createElement('div');
div.appendChild(img);
div.appendChild(deleteButton);
$('#img_preview').append(div);
};
reader.readAsDataURL(file);
}
this.value = null;
});
</script>
....
<button type="button" class="btn btn-primary sub_form">save</button>
</form>
<script>
$(document).on('click', ".sub_form", function () {
document.getElementById('my_form').submit();
});
</script>
Once, I tried to create the input to store these images and I used $(#input_files).val(imageMap)
But it didn’t work.
2
Answers
I didn't find any solutions after I tried for 8 hours but I found it after I asked in the stackoverflow for 10 minutes. This is quite strange.
Just use the
DataTransfer
.First, add a name for file input.
After this, transfer files with the DataTransfer before submitting this form.
Clever idea using the DataTransfer object. Apparently that is the only kind of object that implements the FileList interface and at the same time has methods for adding elements/files.
An alternative approach could be to use the FormData object and then send the data using AJAX aka. using the fetch method (and then clear the form/list of images when the request is successful).