我需要在c#ASP.NET Core Web API应用程序中接收内容和字节数组。
NP <- read.csv("tmp", header = T)
library(ggplot2)
ggplot(NP, aes(as.numeric(Sample), ActB)) +
geom_point() +
geom_smooth(method = "lm", color = "red") +
scale_x_continuous(breaks = 1:nrow(NP), labels = NP$Sample) +
labs(x = "Sample") +
theme(axis.text.x = element_text(angle = 70, hjust = 1))
&#13;
答案 0 :(得分:3)
您可以修改您的控制器操作方法,如下所示,根据需要接受ByteArrayContent
或MultipartFormDataContent
。我在POST中使用了[FromBody]
属性进行模型绑定。
以下是[FromBody]
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding
public class FileController : Controller
{
[HttpPost]
public async Task<IActionResult> Post([FromBody] MultipartFormDataContent content)
{
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
FilePart part = null;
// access the content here
await content.ReadAsMultipartAsync(provider);
// rest of the code
}
}
如前所述,您应该使用content
发布到此API,如下所示
var result = client.PostAsync("/api/file", content).Result;
答案 1 :(得分:2)
我过去曾成功使用过这个:
[HttpPost]
public async Task<IActionResult> Post(IFormFile formFile)
{
using (Stream stream = formFile.OpenReadStream())
{
//Perform necessary actions with file stream
}
}
我相信您还需要更改客户端代码以匹配参数名称:
fileContent.Headers.Add("name", "formFile");