如何在ASP.NET Core Web API Controller中接收字节数组和标头内容

时间:2017-09-27 16:00:27

标签: c# visual-studio asp.net-web-api asp.net-core webclient

我需要在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;
&#13;
&#13; 后端我有工作,但无法找到新的asp.net核心控制器解析文件对象和从客户端发布到服务的数据!任何想法都将一如既往地受到高度赞赏......

2 个答案:

答案 0 :(得分:3)

您可以修改您的控制器操作方法,如下所示,根据需要接受ByteArrayContentMultipartFormDataContent。我在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");