title: Validating an uploaded file's ContentType in ASP.NET
body: <p>I'm storing some files in my database and since I'm storing them in binary format and not keeping any other information, I have to make sure that all of them are in the same format so that I'll be able to "serve" them later (If there's a <strong><em>simple</em></strong> way to infer the file type from a byte array, please tell, but that's not the focus here).</p>
  
  <p>So, what I need to do is validate every file that is uploaded to make sure it's on the required format.</p>
  
  <p>I've set up a FieldTemplate with a FileUpload control and a CustomValidator:</p>
  
  <pre><code>&lt;asp:FileUpload ID="FileUpload" runat="server" /&gt;&amp;nbsp;
  
  
  &lt;asp:CustomValidator
      ID="CustomValidator1"
      runat="server"
      ErrorMessage="PDF only."
      ControlToValidate="FileUpload"
      OnServerValidate="CustomValidator1_ServerValidate"&gt;
  &lt;/asp:CustomValidator&gt;
  </code></pre>
  
  <p>What I'm missing is the code to place in that CustomValidator1_ServerValidate method that checks the uploaded file to make sure it's in the right format (PDF in this case).</p>
  
  <p>Thanks in advance.</p>