Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i am trying to upload documents .user can be able to upload theri documents but he/she can be upload images istead of documents and i want to d restrict about this how to apply condition this is my upload code

C#
if (FileUploadControl.PostedFile != null && 
             FileUploadControl.PostedFile.ContentLength 
            > 0)
        {
            if
                (FileUploadControl.FileContent.Length < 100000)
            {
                string filename = 
                Path.GetFileName(FileUploadControl.PostedFile.FileName);
                string folder = Server.MapPath("~/Docfiles/");
                Directory.CreateDirectory(folder);
                FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
                try
                {
                    cc.upload1(Txt_docde.Value, txt_dname.Value, 
              FileUploadControl.FileName, Convert.ToInt32(Docdrop.SelectedValue),
                       Convert.ToInt32(DropDownList2.SelectedValue), 
              Convert.ToString(Session["Login2"]),Convert.ToInt32(Session["UserID"]));
                    StatusLabel.ForeColor = System.Drawing.Color.Green;
                    //StatusLabel.ForeColor = System.Drawing.FontStyle.Bold;
                    StatusLabel.Text = "Success";
                }
                catch
                {
                    StatusLabel.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "Failed";


                }
            }
                else
            {
                 StatusLabel.ForeColor = System.Drawing.Color.Red;
                            Label2.Text = "File Size to big";
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 13-Nov-13 13:05pm    
Look at the tags: C and ASP.NET. You certainly mixed something up. :-)
—SA

You can just add an if statement that checks the file extension. I have done something like this in the past:
if (filename.EndsWith(".doc"))
{
     //do stuff
}

Or,
if (FileUploadControl.FileName.EndsWith(".doc"))
{
     //do stuff
}
 
Share this answer
 
v4
Yes..
Add if else statement on your validation action.
string typedoc = fileupload1.PostedFile.FileName.Split('.').Last();
if(typedoc == "doc" || typedoc == "docx")
{
    //save
}
else
{
    //alert error with massage box or label
}
 
Share this answer
 
v3
hi either you for the solution suggested above or you can use the below regular expression for validation
XML
<asp:RegularExpressionValidator ID="uplValidator" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage=".doc, .docx formats are allowed"
 ValidationExpression="(.+\.([d][o][c])|.+\.([d][o][c][x]))"></asp:RegularExpressionValidator>
 
Share this answer
 
Comments
Diya Ayesa 14-Nov-13 5:37am    
thankx

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