在ASP.NET Controller中接受二进制数据的方法之一是使用HTTP POST请求并将数据放在请求体中。以下是一个示例代码,演示如何在ASP.NET Controller中接受二进制数据:
[HttpPost] public IActionResult ReceiveBinaryData() { // Get the binary data from the request body byte[] data; using (var ms = new MemoryStream(2048)) { Request.Body.CopyTo(ms); data = ms.ToArray(); }
// Do something with the binary data
// ...
return Ok();
}
在这个示例代码中,我们使用了MemoryStream类来在Controller中接收二进制数据。它允许我们读取请求体中的字节并将其保存在内存流中。
最后,我们只需要根据需要对接收到的二进制数据进行处理即可。通过这种方法,我们可以轻松地在ASP.NET Controller中接受二进制数据。