我有我的API控制器中指定的所有表單數據的問題。具有多個文件到Web API控制器的自定義表單數據
JavaScript的上傳功能:
$scope.upload[index] = $upload.upload({
url: '/api/upload/',
method: 'POST',
data: {
Photographer: $scope.models[index].photographer,
Description: $scope.models[index].desc
},
file: $scope.models[index].file
})
表單數據: 工程,我想它,則發送它包含了我的價值觀,我想它的每個請求。
------WebKitFormBoundaryzlLjAnm449nw1EvC
Content-Disposition: form-data; name="Photographer"
Scott Johnson
------WebKitFormBoundaryzlLjAnm449nw1EvC
Content-Disposition: form-data; name="Description"
Image taken with a Nikon camerea
------WebKitFormBoundaryzlLjAnm449nw1EvC
Content-Disposition: form-data; name="file"; filename="moxnes.jpg"
Content-Type: image/jpeg
我的Web API控制器:
模板從this guide
public class UploadController : ApiController
{
public async Task <HttpResponseMessage> PostFormData()
{
var root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// Show all the key-value pairs.
foreach(var key in provider.FormData.AllKeys)
{
foreach(var val in provider.FormData.GetValues(key))
{
var keyValue = string.Format("{0}: {1}", key, val);
}
}
foreach(MultipartFileData fileData in provider.FileData)
{
var fileName = fileData.Headers.ContentDisposition.FileName;
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
這裏的問題:控制器可以通過這個循環異步接收多個請求和讀取所有文件:foreach(MultipartFileData fileData in provider.FileData)
這工作正常,但我的其他表單數據值(Phtographer和說明)只包含其中一個請求(收到的最後一個請求)的值。
foreach(var key in provider.FormData.AllKeys)
我需要取出每個請求的表格數據值。我該怎麼做,或者有什麼更好的辦法來解決這個問題?也許通過添加模型作爲參數?