How to allow option to select deselect files during file upload

Asked By 10 points N/A Posted on -
qa-featured

I am working on a server project where I need to collect data from the user in the form of .dat file and I have to process it and display the result online. The problem I am facing is once the user uploads the file there is no issue. But, assume user clicks on add files button, he selects multiple files and before clicking on upload button if he or she feels that one or some files in the selected list has to be removed, it cannot be done. Is this a known limitation? Is it browser dependant? Are there any solutions for the above said issue? Thank you for you time.

SHARE
Answered By 565010 points N/A #194213

How to allow option to select deselect files during file upload

qa-featured

Here’s a much easier way of uploading a file or files that allows you to select and deselect files to upload. This method uses HTML forms to upload files and give the user the option to select and deselect files to upload and then passes on the process of uploading through a PHP file. To make it easier to select and deselect files when uploading, you need to use IDs.

<form action="upload_file.php" method="post" onsubmit="return validateForm()" enctype="multipart/form-data">
    <label for="file">Files:</label>
    <input id="file1" type="file" name="file[]" />
    <button id="rmv1" type="button">Remove File</button>

    <input id="file2" type="file" name="file[]" />
    <button id="rmv2" type="button">Remove File</button>

    <input type="submit" name="submit" value="Submit">
</form>

After creating the HTML upload form, you need to add a JavaScript to restore default values.

document.getElementById('rmv1').onclick = function() {
    var file = document.getElementById("file1");
    file.value = file.defaultValue;
}

For the other button, just replace rmv1 with rmv2 and file1 with file2.

Login/Register to Answer

Related Questions