简体   繁体   English

过滤特定文件类型的动态添加的文件上传字段

[英]Filter dynamically added file upload fields for specific file types

I am adding file upload fields using JavaScript. 我正在使用JavaScript添加文件上传字段。

I need to filter the files so that only PDF files can be uploaded. 我需要过滤文件,以便只能上传PDF文件。

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. 警告 :用Javascript进行检查非常非常糟糕。 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. 要接受某种类型的文件,请查看MIME文件类型和accept属性。 Example site: http://www.w3schools.com/tags/att_input_accept.asp 范例网站: http : //www.w3schools.com/tags/att_input_accept.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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