'Upload a file via POST using RestSharp

I'm trying to simulate a simple multipart HTML form in C#.

I was successful posting text as parameters using the addParameter function. Now I want to upload a file using POST. I tried using the addFile function of the RestRequest object. But it did not work! I'm getting response code: 0.

Here's my code:

//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
     if (response.StatusCode == HttpStatusCode.OK)
     {
          //POST successful
          MessageBox.Show("Success!");                   
     }
     else
     {
          //error ocured during POST
          MessageBox.Show(":-(\nFailed.\nError: " + response.ErrorMessage);
     }
});

Please help me find mistakes in my code. Thanks in advance!



Solution 1:[1]

Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheKrush