title: Save file uploaded to server as binary string from javascript to file using c#
body: <p>I'm using js File API and reading file with FileReader like this:</p>
  
  <pre><code>var reader = new FileReader();
  reader.onload = handleReaderLoad;
  reader.readAsBinaryString(file);
  </code></pre>
  
  <p>and this is reader load handler in which I get file content and send it to server using jquery.ajax call:</p>
  
  <pre><code>function handleReaderLoad(evt) {
          fileToSend.Content = evt.target.result;
  
          $.ajax({
              url: '@Url.Action("Upload")',
              type: 'POST',
              contentType: 'application/json',
              data: JSON.stringify({ file: fileToSend }),
              success: function (result) {
                  alert('success');
              }
          });
      }
  </code></pre>
  
  <p>on server side I have:</p>
  
  <pre><code>[HttpPost]
          public string Upload(UploadedFile file)
          {
              // save file
              try
              {
                  FileStream fs = new FileStream(@"c:\" + file.Name, FileMode.Create);
                  BinaryWriter bw = new BinaryWriter(fs);
                  bw.Write(Encoding.UTF8.GetBytes(file.Content));
              }
              catch (Exception) { }
              return null;
          }
  </code></pre>
  
  <p>and the UploadedFile is:</p>
  
  <pre><code>public class UploadedFile
      {
          public string Name { get; set; }
          public string Content { get; set; }
      }
  </code></pre>
  
  <p>I managed to save file, but content is different. I know that it has something to do with encoding but I just don't get same file on server. Can you please tell what am I doing wrong?</p>