2

I am looking for a simple way to check the extension of the uploaded file, if it was PDF file do something, else a warning message will show that its (wrong file type), but the problem with my code is if I selected any file type instead of PDF it will show the error page with this message:

   Server Error in '/' Application.
   PDF header signature not found.
   Exception Details: iTextSharp.text.exceptions.InvalidPdfException: PDF header signature not found.



        <asp:FileUpload runat="server" ID="file1" AllowMultiple="true" />


        string fileName = Path.GetFileName(file1.FileName);
        FileInfo fi = new FileInfo(fileName);
        string ext = fi.Extension;

        if (ext == ".pdf")
        {
        //do something
        }
        else
        Label1.Text = string.Format("wrong file type");
azza
  • 77
  • 1
  • 10
  • Do as Anonymous coward says at http://stackoverflow.com/questions/10621936/itextsharp-exception-pdf-header-signature-not-found – J.SMTBCJ15 Mar 08 '17 at 06:36

2 Answers2

8

to get FileName of the uploaded file

string FileName = file1.PostedFile.FileName;

to get extension of the uploaded file

string FileExtension = System.IO.Path.GetExtension(file1.PostedFile.FileName);
ammu
  • 258
  • 3
  • 9
3
 bool isValidFile = false;

            string[] validFileTypes = { "xlsx", "xls", "pdf" };
            string ext = Path.GetExtension(File_Uploader.PostedFile.FileName);

            for (int i = 0; i < validFileTypes.Length; i++)
            {
                if (ext == "." + validFileTypes[i])
                {
                    isValidFile = true;
                    break;
                }
            }
Saurabh
  • 1,505
  • 6
  • 20
  • 37