我正在使用 Visual Studio 2010,代码不起作用 FileData 并且 FormData 为空。我用萤火虫检查了帖子,它有值。
我的 nuget 包版本是 ASP.NET Web API 4.0.20710.0
我从这里“http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2”举了一个例子
请参阅下面的图片
http://screencast.com/t/5fhj0CfgiG
http://screencast.com/t/KCsPizDmC
“http://screencast.com/t/hhGFGaSjV” ---> 发布图片
当我使用 HttpContext.Current.Request 时,我得到了变量。
我测试了框架 4.5 的应用程序并且工作正常,我认为可能是框架 4 的问题。
我想可能是我的视觉工作室,所以我在另外 3 台电脑上测试它,我遇到了同样的问题。
谢谢
[HttpPost]
public Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
List<string> files = new List<string>();
List<string> formData = new List<string>();
string root = HttpContext.Current.Server.MapPath("~/App_Data");
MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root);
Collection<HttpContent> values = provider.Contents;
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
files.Add(file.Headers.ContentDisposition.FileName);
files.Add("Server file path: " + file.LocalFileName);
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
// Show all the key-value pairs.
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
formData.Add(string.Format("{0}: {1}", key, val));
Trace.WriteLine(string.Format("{0}: {1}", key, val));
}
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}