title: How to upload file to server with HTTP POST multipart/form-data?
body: <p>I am developing Windows Phone 8 app. I want to upload SQLite database via PHP web service using HTTP POST request with MIME type multipart/form-data &amp; a string data called "userid=SOME_ID". </p>
  
  <p>I don't want to use 3rd party libs like HttpClient, RestSharp or MyToolkit. I tried the below code but it doesn't upload the file &amp; also doesn't give me any errors. It's working fine in Android, PHP, etc so there's no issue in web service. Below is my given code (for WP8). what's wrong with it?</p>
  
  <p>I've googled and I'm not getting specific for WP8</p>
  
  <pre class="lang-csharp prettyprint-override"><code>async void MainPage_Loaded(object sender, RoutedEventArgs e)
  {
      var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(DBNAME);
      //Below line gives me file with 0 bytes, why? Should I use 
      //IsolatedStorageFile instead of StorageFile
      //var file = await ApplicationData.Current.LocalFolder.GetFileAsync(DBNAME);
      byte[] fileBytes = null;
      using (var stream = await file.OpenReadAsync())
      {
          fileBytes = new byte[stream.Size];
          using (var reader = new DataReader(stream))
          {
              await reader.LoadAsync((uint)stream.Size);
              reader.ReadBytes(fileBytes);
          }
      }
  
      //var res = await HttpPost(Util.UPLOAD_BACKUP, fileBytes);
      HttpPost(fileBytes);
  }
  
  private void HttpPost(byte[] file_bytes)
  {
      HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.myserver.com/upload.php");
      httpWebRequest.ContentType = "multipart/form-data";
      httpWebRequest.Method = "POST";
      var asyncResult = httpWebRequest.BeginGetRequestStream((ar) =&gt; { GetRequestStreamCallback(ar, file_bytes); }, httpWebRequest);  
  }
  
  private void GetRequestStreamCallback(IAsyncResult asynchronousResult, byte[] postData)  
  {
      //DON'T KNOW HOW TO PASS "userid=some_user_id"  
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;  
      Stream postStream = request.EndGetRequestStream(asynchronousResult);  
      postStream.Write(postData, 0, postData.Length);  
      postStream.Close();  
      var asyncResult = request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);  
  }  
  
  private void GetResponseCallback(IAsyncResult asynchronousResult)  
  {  
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;  
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);  
      Stream streamResponse = response.GetResponseStream();  
      StreamReader streamRead = new StreamReader(streamResponse);  
      string responseString = streamRead.ReadToEnd();  
      streamResponse.Close();  
      streamRead.Close();  
      response.Close();  
  }  
  </code></pre>
  
  <p>I also tried to solve my problem in Windows 8 but it's also not working.</p>
  
  <pre class="lang-csharp prettyprint-override"><code>public async Task Upload(byte[] fileBytes)
  {
      using (var client = new HttpClient())
      {
          using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture)))
          {
              content.Add(new StreamContent(new MemoryStream(fileBytes)));
              //Not sure below line is true or not
              content.Add(new StringContent("userid=farhanW8"));
              using (var message = await client.PostAsync("http://www.myserver.com/upload.php", content))
              {
                  var input = await message.Content.ReadAsStringAsync();
              }
          }
      }
  }
  </code></pre>