vandan's avatar
Level 13

How to file upload with restriction ?

i was try to upload file but when i click upload button to select custom file or all files now how to restrict all file option when i click upload button and only show in dialog box and choose specfic file when i was validate

please suggest thank you

0 likes
8 replies
vandan's avatar
Level 13

@TRAY2 - Thank for reply but i was try to disable all file option while file upload so please suggestion how to fix it

jlrdw's avatar

@VAN1310 - Did you not read over that link it shows Additional attributes.

Quote

<input type="file" id="docpicker"
  accept=".doc,.docx,application/msword,application/vnd.openxmlformat

Unquote

And I remember another post with the same question snappy told you that's probably as close as you're going to get. You cannot control every browser.

I suppose you could write your own code either something using jQuery or some other JavaScript or perhaps your own c++ class.

I doubt anyone's going to give you a complete cut and paste solution , you may have to take a few months and learn how to code that stuff.

vandan's avatar
Level 13

@JLRDW - yes i read this but i click upload button and open a dialog box and open a dropdown list which is custom file and also all files when i click dropdown so i try to disable all file option only show a specifie file please suggest me

vandan's avatar
vandan
OP
Best Answer
Level 13

i did this using javascript

var _validFileExtensions = [".jpg", ".jpeg", ".docx", ".xlsx", ".pdf"];    

function Validate(oForm) 
{
        var arrInputs = oForm.getElementsByTagName("input");
        for (var i = 0; i < arrInputs.length; i++) 
    {    
        var oInput = arrInputs[i];
            if (oInput.type == "file") {
                    var sFileName = oInput.value;
                    if (sFileName.length > 0) {
                        var blnValid = false;
                        for (var j = 0; j < _validFileExtensions.length; j++) 
                {
                                var sCurExtension = _validFileExtensions[j];
                                
                    if (sFileName.substr(sFileName.length - sCurExtension.length,                               sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) 
                {
                                blnValid = true;
                                break;
                            }
                    }
            
                    if (!blnValid) 
            {
                            alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                            return false;
                    }
                }
        }
    } 
return true;

}

vandan's avatar
Level 13

@JLRDW - i best answer click self beacuse many people search query like this so not waste your time then i was click best answer @jlrdw thanks for reply next time i be carefully question ask

Please or to participate in this conversation.