【发布时间】:2021-10-19 01:48:01
【问题描述】:
我想将图像发送到 api 端点和端点接受字节 [] 我该如何处理。 我的代码是: 上传按钮点击 ReactJS 函数:
let onClickHandler = () => {
let data = new FormData()
data.append('file', image);
axios.post("/Contact/ContactForm/", {
AttachedImage: data
}, {
headers: {
'Content-Type': 'multipart/form-data',
'X-Requested-With': 'XMLHttpRequest',
}
// receive two parameter endpoint url ,form data
})
.then(res => { // then print response status
console.log('binary Response......',res.statusText)
});
}
和我的端点控制器方法:
[HttpPost]
public async Task<IActionResult> ContactForm([FromBody] Contact model)
{
try
{
model.CreationDate = DateTime.Now;
await _contact.ContactForm(model);
return Ok("Submitted Successfully");
}
catch (Exception ex) { return this.BadRequest("Not Submitted Successfully" + ex.ToString()); }
}
最后是 Model.cs 类
public byte[] AttachedImage { set; get; }
【问题讨论】:
-
@BlackSheep 不,这对我没有帮助。
-
您可以尝试在帖子中更改方法,可能对您有所启发:stackoverflow.com/questions/16363419/…
标签: javascript reactjs axios asp.net-core-webapi binaryfiles