I have a form which has file upload input
<form action="abc.php" method="post">
<input name="fileToUpload" type="file" />
<div id="view"></div>
<button type="submit" value="submit" name="submit">Submit</button>
</form>
I wish to fetch the name of the file/image uploaded on click of this input and display it under <div id="view"></div>
, but this needs to be done before the form submission. can anyone please tell how this can be done
Try to update your code to following,
<form action="abc.php" method="post">
<input name="fileToUpload" id="files" type="file" />
<div id="view"></div>
<button type="submit" value="submit" name="submit">Submit</button>
</form>
<script>
var control = document.getElementById("files");
control.addEventListener("change", function(event) {
// When the control has changed, there are new files
var i = 0,
files = control.files,
len = files.length;
for (; i < len; i++) {
document.getElementById('view').innerHTML = files[i].name;
}
}, false);
</script>
I hope this helps.
Try this code, it will surely help
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
Script Code
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'</li>');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.