Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

i have one fileupload control need to validate from client side accept only pdf.
if i try to upload other file except pdf should give alert and need to remove selected path

please give me solution.

C#
<asp:FileUpload ID="FileUplod1" runat="server" Height="18px" onclientclick="CheckFile(this);" />



JavaScript
function CheckFile(Cntrl) {
        var file = document.getElementById(Cntrl.name);
        var len = file.value.length;
        var ext = file.value;
        if (ext.substr(len - 3, len) != "pdf")
        {
            alert("Please select a doc or pdf file ");
            return false;
        }
    }
Posted

I have running example and tried provide required info. please use below code

ASP.NET
<asp:asyncfileupload onuploadedcomplete="FileUploadComplete" xmlns:asp="#unknown">
                                    runat="server" ID="AsyncFileUploadImage" Width="400px" UploaderStyle="Modern"
                                    CompleteBackColor="White" UploadingBackColor="#0A7AC5" ThrobberID="imgloading"
                                    OnClientUploadError="uploadError" CssClass="fileupload" TabIndex="6" OnClientUploadStarted="uploadImageStart" /></asp:asyncfileupload>

JavaScript
function uploadImageStart(sender, args) {
    var filename = args.get_fileName();

    var filext = filename.substring(filename.lastIndexOf(".") + 1);
    filext = filext.toLowerCase();
    if (filext == "pdf") {
        return true;
    }
    else {
        var err = new Error();
        err.name = 'Fill Ext Error';
        err.message = 'Not valid file. Only .pdf files allowed';
        throw (err);
        return false;
    }
}

function uploadError(sender, args) {
    alert(args.get_errorMessage());

}


FileUploadComplete - code behind
 
Share this answer
 
for client side javascript


JavaScript
<script type="text/javascript">
    var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];
    function ValidateFile()
    {
      var file = document.getElementById("<%=FileUpload1.ClientID%>");
      var label = document.getElementById("<%=Label1.ClientID%>");
      var path = file.value;
      var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
      var isValidFile = false;
      for (var i=0; i<validfilestypes.length;>      {
        if (ext==validFilesTypes[i])
        {
            isValidFile=true;
            break;
        }
      }
      if (!isValidFile)
      {
        label.style.color="red";
        label.innerHTML="Invalid File. Please upload a File with" +
         " extension:\n\n"+validFilesTypes.join(", ");
      }
      return isValidFile;
     }
</script></script>




code behind file
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    string[] validFileTypes={"bmp","gif","png","jpg","jpeg","doc","xls"};
    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    bool isValidFile = false;
    for (int i = 0; i < validFileTypes.Length; i++)
    {
        if (ext == "." + validFileTypes[i] )
        {
            isValidFile = true;
            break;
        }
    }
    if (!isValidFile)
    {
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "Invalid File. Please upload a File with extension " +
                       string.Join(",", validFileTypes);
    }
    else
    {
        Label1.ForeColor = System.Drawing.Color.Green;
        Label1.Text = "File uploaded successfully.";
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900