简体   繁体   中英

Filter dynamically added file upload fields for specific file types

I am adding file upload fields using JavaScript.

I need to filter the files so that only PDF files can be uploaded.

Here is what I have so far :

function AddFileUpload() {
    var div = document.createElement('DIV');
    div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
    '" type="file" />' +
    '<input id="Button' + counter + '" type="button" ' +
    'value="Remove" onclick = "RemoveFileUpload(this)" />';
    document.getElementById("FileUploadContainer").appendChild(div);
    counter++;
}

function RemoveFileUpload(div) {
    document.getElementById("FileUploadContainer").removeChild(div.parentNode);
}

WARNING : Doing this check with Javascript is very, VERY bad. It can be manipulated very easily, and it is definitely not recommended. At all. By anyone. Don't do it.

function AddFileUpload() {
    var div = document.createElement('DIV');
    div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
        '" type="file" accept="application/pdf"/>' +
        '<input id="Button' + counter + '" type="button" ' +
        'value="Remove" onclick = "RemoveFileUpload(this)" />';
    document.getElementById("FileUploadContainer").appendChild(div);
    counter++;
}

function RemoveFileUpload(div) {
    document.getElementById("FileUploadContainer").removeChild(div.parentNode);
}

To accept a certain type of file, look into MIME file types and the accept attribute. Example site: http://www.w3schools.com/tags/att_input_accept.asp

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM