title: How to properly save a file in data URL format to IsolatedStorage
body: <p>I am trying to upload a file from the browser to an asp.NET service. I am using <code>FileReader.readAsDataURL()</code> and getting the file as data URL.</p>
  
  <p>I am using this JavaScript code <code>event.target.result.match(/,(.*)$/)[1]</code> to get only my file in base64 format. This is what I am sending to the service.</p>
  
  <p>Using the code below I am storing the file in isolated storage</p>
  
  <pre class="lang-cs prettyprint-override"><code>IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(
       IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
  
  byte[] byteArray = Convert.FromBase64String(data);                    
  
  IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile", 
        FileMode.Create, isoStore);
  
  StreamWriter writer = new StreamWriter(stream);
  writer.Write(byteArray);
  writer.Close();
  </code></pre>
  
  <p>Unfortunately, the file that I am saving is corrupted. Am I doing something wrong? Is there a better method for doing this?</p>
  
  <p>Edit:
  I am trying to achieve Gmail style file upload. I have tried with forms but it complicates things to much.</p>
  
  <p>My ajax call looks like this:</p>
  
  <pre><code>var query = {
      "data": fileData,
      "fileName": fileName
  };
  
  $.ajax({
      type: "POST",
      url: "Page.aspx/UploadFile",
      data: JSON.stringify(query),
      contentType: "application/json; charset=utf-8",
      dataType: "JSON"});
  </code></pre>
  
  <p>Can it be something related to the UTF-8 encoding?</p>