我正在使用Visual Studio 2010,代码不工作FileData和FormData为空。我用萤火虫检查邮政,它有价值。ASP.NET Web API上传多部分MIME
我的NuGet包的版本的ASP.NET Web API 4.0.20710.0
我从这里的例子“http://www.asp.net/web-api/overview/working-with- HTTP /送的HTML形式的数据,-part-2"
参见下面
http://screencast.com/t/5fhj0CfgiG
http://screencast.com/t/KCsPizDmC
图像的“http:// SC reencast.com/t/hhGFGaSjV“---> POST与图像
当我使用HttpContext.Current.Request我得到的变量。
我测试框架4.5的应用程序和工作正常,我想也许是框架问题4.
我想可能是我的Visual Studio,所以我测试它在其他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;
}