3

I have a ASP website page where i have a upload control added

<asp:FileUpload ID="FileUpload1" runat="server" 
                        BorderStyle="None" Width="215px" onchange="return checkfile();" style="margin-left: 14px" BackColor="#F0F0F0" />

From javascript i am validating the file which will be uploaded. If it is of type .exe then I will not allow to upload and give a message. if not i will display the file name in label "lblFileName" . But the problem if error(in case file is exe) then i want to reset the upload control(FileUpload1) . Now it will show only message but allows form to submit along with the .exe file.So how I can reset it?

 function checkfile() {

     var filename = document.getElementById("FileUpload1").value;
     var lastIndex = filename.lastIndexOf("\\");


     if (lastIndex >= 0) {
         filename = filename.substring(lastIndex + 1);
     }

    var FileExt = filename.split('.').pop();

    if (FileExt == "exe") {  

        document.getElementById('<%=lblFileName.ClientID%>').innerHTML = "you cannot attach exe file";
    return false;

     }
    else {
        document.getElementById('<%=lblFileName.ClientID%>').innerHTML = filename;
    }
  }

enter image description here

1
  • this link has some good tricks. Commented Jan 15, 2016 at 11:50

2 Answers 2

6

Your code is the problem onchange="return checkfile();"

And your function should look like

    function checkfile() {

         var filename = document.getElementById("FileUpload1").value;
         var lastIndex = filename.lastIndexOf("\\");


         if (lastIndex >= 0) {
             filename = filename.substring(lastIndex + 1);
         }

        var FileExt = filename.split('.').pop();

        if (FileExt == "exe") {         
            document.getElementById('lblFileName').innerHTML = "you cannot attach exe file";
            document.getElementById("FileUpload1").value='';
            return false;

         }
         else {
            document.getElementById('lblFileName').innerHTML = filename;
        }
      }

Return will disallow file to put in your file upload control so this will solve your problem

Please check demo here

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried the same script you mentioned ,but it didnt work. check screenshot attached in question
Hello please add this line document.getElementById("FileUpload1").value=''; i have put in my function check above function you forgot to add this line
0

I am doing it this way using jquery:

    $(function () {        
    $('<%= fileUploadCV.ClientID %>').change(function () {

        //because this is single file upload I use only first index
        var f = this.files[0]

        //here I check if the file size is bigger than 8 MB (numbers below are in bytes)
        if (f.size > 8388608 || f.fileSize > 8388608)
        {
           //show an alert to the user
           alert("Allowed file size exceeded. (Max. 8 MB)")

           //and RESET file upload control
           this.value = null;
        }
    })
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.