I'm trying to upload a file and some key value pairs to a server.
The IT guy of the server tells me "When our server is receiving the request the file is showing up great, but none of the data in your key value pairs are showing up in the request (as it’s parsed by ASP.Net). I haven’t determined exactly what’s going on with that yet, but I suspect it’s getting buried by being in the dataContent object one layer deeper in the content object."
The code I'm using is below. How can I move the key value pairs "up a level"? I'm not all that savvy with networking, unfortunately.
using (var content = new MultipartFormDataContent())
{
var dataContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Key1", "Value1"),
new KeyValuePair<string, string>("Key2", "Value2")
});
content.Add(dataContent);
using (var fileStream = File.OpenRead(filePathAndName))
using (var fileStreamContent = new StreamContent(fileStream))
{
fileStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "File",
FileName = Path.GetFileName(filePathAndName)
};
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Add(fileStreamContent);
using (var response = await client.PostAsync("api/datasets", content))
{
if (response.IsSuccessStatusCode)
{
Debug.WriteLine("Upload Success");
}
else
{
Debug.WriteLine("Upload Failed");
Debug.WriteLine("Response : " + response);
}
}
}
}
Instead of using a FormUrlEncodedContent, I also tried
content.Add(new StringContent("Value1"), "\"Key1\"");
content.Add(new StringContent("Value2"), "\"Key2\"");
but no luck