将键值对添加到 MultipartFormDataContent
Add key value pairs to MultipartFormDataContent
我正在尝试将文件和一些键值对上传到服务器。
服务器的IT小哥告诉我"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."
我使用的代码如下。如何移动键值对 "up a level"?不幸的是,我对网络不是那么精通。
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);
}
}
}
}
我也试过
,而不是使用 FormUrlEncodedContent
content.Add(new StringContent("Value1"), "\"Key1\"");
content.Add(new StringContent("Value2"), "\"Key2\"");
但运气不好
原来是服务器端的问题
我正在尝试将文件和一些键值对上传到服务器。
服务器的IT小哥告诉我"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."
我使用的代码如下。如何移动键值对 "up a level"?不幸的是,我对网络不是那么精通。
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);
}
}
}
}
我也试过
,而不是使用 FormUrlEncodedContentcontent.Add(new StringContent("Value1"), "\"Key1\"");
content.Add(new StringContent("Value2"), "\"Key2\"");
但运气不好
原来是服务器端的问题