Home > ASP.Net, Javascript > Allow User to Upload only .doc file

Allow User to Upload only .doc file

check this example with server side :

if (FileUpload1.HasFile == true && FileUpload1.PostedFile != null)
            int _size = 9437184;// equal 9 mb
            string _fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (_fileExt.ToLower() == ".doc" || _fileExt.ToLower() == ".docx" )
                if (FileUpload1.PostedFile.ContentLength <= _size)
                    string CarFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName);
                    FileUpload1.SaveAs(Request.PhysicalApplicationPath + "Car_Image\" + CarFile);                    

                    ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type="text/javascript">alert('Max file size is 9 m.b');</script>");
                ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type="text/javascript">alert('Only doc or docx are allowed');</script>");
            //-- if person not has image set default  for him


 Or try this using Validator Control:

try this example to allow user to upload file with Doc extension

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                    ErrorMessage="Only JPG/jpg file allowed!"
                    ValidationExpression="^(([a-zA-Z]: )|(<a>\{2}w+)$?)(\(w[w](.)*))+(.doc|.DOC)$</a>"
                    Width="510px" CssClass="Validator"></asp:RegularExpressionValidator>

//—–or —–

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                    ErrorMessage="Only doc file allowed!"
                    ValidationExpression="^(([a-zA-Z]: )|(<a>\{2}w+)$?)(\(w[w].*))+(.doc|.DOC)$</a>"
                    Width="510px" CssClass="Validator"></asp:RegularExpressionValidator> 

//——-or ——-

  <asp:RegularExpressionValidator ID="imgValidator" runat="server" ControlToValidate="imgUpload"
                ErrorMessage="*doc files only" ValidationExpression="^.+.(([dD][oO][cC]?[xX])|([dD][oO][cC]))$" />

or try this to allow user to upload doc file

<asp:RegularExpressionValidator ID="FileUpLoadValidator" runat="server" ErrorMessage="Only doc files!"
                ValidationExpression=".*(.doc|.DOC)$" ControlToValidate="FileUpload1" Display="None">

//—— or ———

<asp:RegularExpressionValidator ID="FileUpLoadValidator" runat="server" ErrorMessage="Only .doc files!"
                ValidationExpression=".*(\.doc|\.DOC)$" ControlToValidate="FileUpload1" Display="None">

Hope it helps you

Good Luck

Categories: ASP.Net, Javascript Tags:
  1. November 28, 2012 at 4:13 pm

    After looking at a handful of the blog posts on your
    web page, I seriously appreciate your way of blogging.
    I added it to my bookmark website list and will be checking back soon.
    Please visit my web site too and let me know what you

  2. March 15, 2013 at 11:17 am

    hello sir
    i apply one by one these codes but not working file upload insert but not generate error .doc file………….tell me what problem this or my code

    • yasserzaid
      March 16, 2013 at 10:53 am

      @Kaleem Paracha : Did you get any error? can you please send me the code that you are using?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: