Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Upload .pdf file and then check if it s .pdf file or not?
I am using Upload control in asp.net but while I upload any one file error occurred.
so I need Upload coding in C#.

Thanks Advance..
Posted
Updated 19-Jan-11 22:50pm
v3

Just check extension before uploading.

C#
if (FileUpload1.HasFile)
        {
            // Get the name of the file to upload.
            string fileName = Server.HtmlEncode(FileUpload1.FileName);
            // Get the extension of the uploaded file.
            string extension = System.IO.Path.GetExtension(fileName);
            // Allow only files with .doc or .xls extensions
            // to be uploaded.
            if (extension == ".pdf") 
{
//upload accordingly
}
            }
 
Share this answer
 
Comments
Manas Bhardwaj 20-Jan-11 4:57am    
Don't you think its better to write a client side function to do the validation?
RaviRanjanKr 20-Jan-11 4:57am    
Nice Answer! have my +5
PALANI KUMAR.A 1-Feb-11 3:27am    
Thanks..
Its very hard to justify what is error in your code without taking its snap but you can navigate this[^] link to know how to upload file[.pdf] with checking it in ASP.net.
 
Share this answer
 
v2
Use FileUpload control &RegularExpressionValidator to validate.

XML
<asp:FileUpload ID="flUpld" runat="server" />

                <asp:RegularExpressionValidator
                    id="RegularExpressionValidator1" runat="server"
                    ErrorMessage="Only PDF files are allowed!"
                    ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF)$"
                    ControlToValidate="flUpld" CssClass="text-red"></asp:RegularExpressionValidator>
 
Share this answer
 

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

  Print Answers RSS


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