我正在使用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;
}