我有使用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
模型,并且里面的所有道具都为空。
如何执行?
您需要绑定类的每个属性,这就是模型绑定程序的工作方式,它正在寻找类的属性的名称。因此,根据您的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'})