Javascript 使用axios将Fileupload和JsonData对象发送到dotnet core api时出错
这是我的代码 来自axiosJavascript 使用axios将Fileupload和JsonData对象发送到dotnet core api时出错,javascript,c#,asp.net-core,.net-core,axios,Javascript,C#,Asp.net Core,.net Core,Axios,这是我的代码 来自axios onPublishChannelHandler = async () => { const request = { ...this.state.channel }; const formData = new FormData(); formData.append('images', request.images[0]); request.images = []; formData.append('data', JSON.stringify(request
onPublishChannelHandler = async () => {
const request = { ...this.state.channel };
const formData = new FormData();
formData.append('images', request.images[0]);
request.images = [];
formData.append('data', JSON.stringify(request));
const result = await axios.post(
'http://localhost:50634/api/user',
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
}
);
}
在Dotnetcore API上
我的型号
public List<IFormFile> images { get; set; }
public string SelectedPhoto { get; set; }
public bool AgreedToTermsAndCondition { get; set; }
public string CreatorType { get; set; }
public string MobileNo { get; set; }
public string Email { get; set; }
公共列表图像{get;set;}
公共字符串SelectedPhoto{get;set;}
公共bool AgreedToTermsAndCondition{get;set;}
公共字符串创建者类型{get;set;}
公共字符串MobileNo{get;set;}
公共字符串电子邮件{get;set;}
API
[HttpPost]
public async Task<IActionResult> Post([FromForm] ChannelSaveRequest request)
[HttpPost]
公共异步任务Post([FromForm]ChannelSaveRequest请求)
当我从axios发布时,我只得到文件和其他字段为空
当我从axios发布时,我只得到文件和其他字段为空
基于您的ChannelSaveRequest
model类,为了使其正常工作,请在JavaScript客户端修改formData对象,如下所示
var formData = new FormData();
formData.append('images', request.images[0]);
formData.append('SelectedPhoto', request.SelectedPhoto);
formData.append('AgreedToTermsAndCondition', request.AgreedToTermsAndCondition);
formData.append('CreatorType', request.CreatorType);
formData.append('MobileNo', request.MobileNo);
formData.append('Email', request.Email);
此外,如果您不想更改客户端代码,则需要实现一个类似于下面的方法
public class CSRequestModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
// ...
// implement it based on your actual requirement
// code logic here
// ...
var model = new ChannelSaveRequest();
if (bindingContext.ValueProvider.GetValue("data").FirstOrDefault() != null)
{
model = JsonSerializer.Deserialize<ChannelSaveRequest>(bindingContext.ValueProvider.GetValue("data").FirstOrDefault());
model.images.Add(bindingContext.HttpContext.Request.Form.Files[0]);
}
bindingContext.Result = ModelBindingResult.Success(model);
return Task.CompletedTask;
}
}
公共类CSRequestModelBinder:IModelBinder
{
公共任务BindModelAsync(ModelBindingContext bindingContext)
{
if(bindingContext==null)
{
抛出新ArgumentNullException(nameof(bindingContext));
}
// ...
//根据您的实际需求实施它
//这里的代码逻辑
// ...
var model=new ChannelSaveRequest();
if(bindingContext.ValueProvider.GetValue(“数据”).FirstOrDefault()!=null)
{
model=JsonSerializer.Deserialize(bindingContext.ValueProvider.GetValue(“数据”).FirstOrDefault());
model.images.Add(bindingContext.HttpContext.Request.Form.Files[0]);
}
bindingContext.Result=ModelBindingResult.Success(model);
返回Task.CompletedTask;
}
}
将其应用于动作参数
[HttpPost]
公共异步任务发布([FromForm][ModelBinder(BinderType=typeof(CSRequestModelBinder))]ChannelSaveRequest请求)
{
测试结果
嗨@Tejas,关于这个案子有什么新消息吗?@FeiHan有,很有效,谢谢
[HttpPost]
public async Task<IActionResult> Post([FromForm][ModelBinder(BinderType = typeof(CSRequestModelBinder))]ChannelSaveRequest request)
{