I have an HTML form where a user can enter basic data about themselves. The last field is of input
type="file"
. The user can select multiple images from their local disk drive.
I want to store the images in one string with the selected images separated by commas.
Here is my code which is not working:
var input = document.mainForm.imageInput;
input.onchange = function() {
var file = input.files[0],
reader = new FileReader();
reader.onloadend = function() {
var filesSize = 0
for (i = 0; i < input.files.length; i++) {
filesSize += input.files[i].size
}
if ((filesSize / 1024 / 1024) > 100) {
console.log("bigger");
} else {
console.log("smaller");
* // I need to do a for loop here to save all images in one string in
document.mainForm.hiddenImageNameBase64.value *
var b64Transformation = reader.result.replace(/^data:.+;base64,/, '');
console.log(b64Transformation);
document.mainForm.hiddenImageNameBase64.value = b64Transformation;
var allImagesInOneString = ""
for (i = 0; i < input.files.length; i++) {
allImagesInOneString += input.files[i].value.replace(/.*[\/\\]/, '');
}
document.mainForm.imageName.value = allImagesInOneString;
input.classList.remove("is-ok");
input.classList.add("is-notok");
}
};
reader.readAsDataURL(file);
};
<div class="container">
<form class="x" name="mainForm" id="mainForm" method="post" action="https://testpagex.com">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="imageInput" class="font-weight-bold">Add images ( maximum size 100mb )</label>
<input type="file" class="form-control-file" id="imageInput" name="imageInput" multiple />
</div>
</div>
<input type="hidden" name="hiddenImageName">
<input type="hidden" name="hiddenImageNameBase64">
<div class="y">
<button type="submit" class="btn btn-primary px-5 font-weight-bold">Save</button>
</div>
</form>
</div>