---
title: "how to check file format"  
description: "how to check file format"  
author: "Anurag Sharma"  
published: 2014-12-26  
updated: 2014-12-26  
canonical: https://www.mindstick.com/forum/12820/how-to-check-file-format  
category: ".net"  
tags: [".net"]  
reading_time: 2 minutes  

---

# how to check file format

Hi Guys

\
the allowed [file format](https://www.mindstick.com/forum/382/how-to-import-and-export-data-in-sql-server-from-different-file-format) is only [PDF file](https://www.mindstick.com/forum/572/pdf-file-download-in-zip-file-in-mvc-4), how to check the file format and [display error](https://www.mindstick.com/forum/158286/how-to-display-error-messages-using-jquery-validation) [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) if the [uploaded file](https://www.mindstick.com/forum/137/how-to-check-uploaded-file-type-using-asp-dot-net) is not PDF before proceeding to [update database](https://www.mindstick.com/forum/146/update-database-through-windows-service). The following code always displays the file is not recognized even the file is PDF and also the database not updated.

\

```
 string filePath = FileUpload1.PostedFile.FileName;    string filename = Path.GetFileName(filePath);    string ext = Path.GetExtension(filename);    string contenttype = String.Empty;     switch (ext)    {        case ".pdf":             contenttype = "application/pdf";             break;        default:            System.Console.WriteLine("File format not recognised. Only PDF format allowed");            break;    }    if (contenttype != String.Empty)    {        Stream fs = FileUpload1.PostedFile.InputStream;        BinaryReader br = new BinaryReader(fs);        Byte[] bytes = br.ReadBytes((Int32)fs.Length);         string classNmae = ddClass.Text.Split('~')[0] + ddClass.Text.Split('1');        com.Parameters.Clear();        com.CommandText = "UPDATE [Marking] SET [fileName]=@fileName, [fileType]=@fileType, [type]=@typ,[submissionDate]=@sd, [filePath]=@fp where [class_id]=@cid AND [module_id]=@mid  AND [student_id]= '" +Session["id"].ToString() + "'";        com.Parameters.Add("@cid", SqlDbType.VarChar).Value = ddClass.Text.Split('~')[0];        com.Parameters.Add("@mid", SqlDbType.VarChar).Value = ddClass.Text.Split('~')[1];        com.Parameters.Add("@fileName", SqlDbType.VarChar).Value = filename;        com.Parameters.Add("@fileType", SqlDbType.VarChar).Value = "application/pdf";        com.Parameters.Add("@typ", SqlDbType.VarChar).Value = txtType.Text;        com.Parameters.Add("@sd", SqlDbType.VarChar).Value = DateTime.Now;        com.Parameters.Add("@fp", SqlDbType.Binary).Value = bytes;         com.ExecuteNonQuery();     }    else    {         lb.Text = "File format not recognised." +           " Upload Word formats";     }
```

Thanks

## Replies

### Reply by Allen Scott

Try this:\

```
if (FileUpload1.HasFile){    HttpPostedFile myPostedFile = FileUpload1.PostedFile;    FileInfo finfo = new FileInfo(myPostedFile.FileName);    if (finfo.Extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase))    {        //do the operation    }}
```


---

Original Source: https://www.mindstick.com/forum/12820/how-to-check-file-format

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
