title: Detecting encoding of uploaded text file (ASP.NET MVC)
body: <blockquote>
  <p><strong>Possible Duplicate:</strong><br />
  <a href="https://stackoverflow.com/questions/90838/how-can-i-detect-the-encoding-codepage-of-a-text-file">How can I detect the encoding/codepage of a text file</a></p>
  </blockquote>
  
  <p>I have a ASP.NET MVC application. In my view I upload a text file and process it with a controller method with  this signature</p>
  <pre><code>[HttpPost]
  public ActionResult FromCSV(HttpPostedFileBase file, string platform)
  </code></pre>
  <p>I get a stream from the uploaded file as <code>file.InputStream</code> and read it using a standard StreamReader</p>
  <pre><code>using (var sr = new StreamReader(file.InputStream))
  {
      ...
  }
  </code></pre>
  <p>The problem is, that this only works for UTF text files. When I have a text file in Windows-1250, the characters get messed up. I can work with Windows-1250 encoded text files when I explicitly specify the encoding</p>
  <pre><code>using (var sr = new StreamReader(file.InputStream, Encoding.GetEncoding(1250)))
  {
      ...
  }
  </code></pre>
  <p>My problem is, that I need to support both UTF and Windows-1250 encoded files so I need a way to detect the encoding of the submitted file.</p>