我有使用React Native制作的应用程序,后端API在.NET C#中。我正在尝试从前端向后端发送一些数据
reactjs
let formData = new FormData();
formData.append('token', token)
formData.append('document', document)
formData.append('file', file);
哪里token
是字符串,file
是一些文件,但是document
带有参数的对象,例如Id
和Name
。所以在后端我收到这样的数据
C#
[HttpPost]
[AllowAnonymous]
public ActionResult SendDocument(string token, DocumentMobile document, HttpPostedFileBase file)
{
//do thins
}
问题在于对象document
不会DocumentMobile
像以前不使用那样转换为模型FormData
,并且里面的所有道具都是null。
这该怎么做?
您需要绑定类的每个属性,这就是模型绑定程序的工作方式,它正在寻找类的属性的名称。因此,根据您的document
类的结构,以下情况之一应适用于您的情况:
formData.append('Id', document.Id)
formData.append('Name', document.Name)
或这个:
formData.append('document', {Id: document.Id, Name: document.Name})
要么:
formdata.append("document[id]", document.Id)
formdata.append("document[name]", document.Name)
对于文件,您可能想要使用以下内容:
formData.append('file', {uri: file, name: 'image.jpg', type: 'image/jpeg'})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句