[英]Asp.net WebApi 2 File upload with Model Object
項目是(Azure上的.net Framework 4.6.1)是否有人知道是否有一種方法可以使用Controller在同一請求中接受文件上載和模型實體(Json)。 客戶端應用需要上傳文件以及有關該文件的元數據,如果可能,我們希望在一個請求中完成。
我想到了兩種可能性:
我是否缺少選擇? TIA
我要嘗試的只是將文件添加到模型中,或者使用其中的數據和文件創建一個ViewModel。 由於數據綁定,只要您在客戶端中的輸入字段具有相同的名稱,該數據就會在請求期間映射到該對象。
模型:
public class yourModel
{
public string name {get;set;}
...
//other data
public IFormFile yourFile {get;set;} //your file
...
}
控制器:
[Route("YourRoute")]
[HttpPost]
public async Task<WhateverYouWantToReturn> YourAction(yourModel model)
{
//set a breakpoint here to see if your fields populated
...
//do something with your model
...
return WhateverYouWantToReturn;
}
視圖:只要輸入字段的名稱與對象中字段的名稱匹配,它們就應該映射。
<form enctype="multipart/formdata" method="post" action="/your/route">
<input type="text" name="name"/>
<input type="hidden" name="somesHiddenField" value="yourValue" />
... <!-- Whatever other fields you need. -->
<input type="file" name="yourFile"/>
<input type="submit" value="Submit" />
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.