如何在 ASP.NET Core Web API 控制器中接收字节数组和标头内容
Posted
技术标签:
【中文标题】如何在 ASP.NET Core Web API 控制器中接收字节数组和标头内容【英文标题】:How to receive a byte array and header content in a ASP.NET Core Web API Controller 【发布时间】:2018-03-09 04:59:57 【问题描述】:我需要在 c# ASP.NET Core Web API 应用程序中接收内容和字节数组。
**--HttpClient (console Application)**
Dictionary<int, byte[]> fileparts = new Dictionary<int, byte[]>();
int bufferSize = 1000 * 1024;
byte[] buffer;
string filePath = @"D:\Extra\Songs\test.mp3";
using (FileStream fileData = File.OpenRead(filePath))
int index = 0;
int i = 1;
while (fileData.Position < fileData.Length)
if (index + bufferSize > fileData.Length)
buffer = new byte[(int)fileData.Length - index];
fileData.Read(buffer, 0, ((int)fileData.Length - index));
else
buffer = new byte[bufferSize];
fileData.Read(buffer, 0, bufferSize);
fileparts.Add(i, buffer);
index = (int)fileData.Position;
i++;
while (fileparts.Count() != 0)
var data = fileparts.First();
var fileContent = new ByteArrayContent(data);//byte content
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") FileName = "test" ;
fileContent.Headers.Add("id", id);
fileContent.Headers.Add("name", name);
fileContent.Headers.Add("length", length);
if (fileparts.Count() == 1)
fileContent.Headers.Add("IsCompleted", "true");
else
fileContent.Headers.Add("IsCompleted", "false");
using (var content = new MultipartFormDataContent())
content.Add(fileContent);
// Make a call to Web API
var result = client.PostAsync("/api/file", fileContent).Result;
if (result.StatusCode == System.Net.HttpStatusCode.OK)
fileparts.Remove(data.Key);
**--WebApi Core ApiController**
public class FileController : Controller
[HttpPost]
public async Task<IActionResult> Post()
/*i need to get the content here , for some reason existing .NET libraries does not work here
like
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
FilePart part = null;
await Request.Content.ReadAsMultipartAsync(provider); -- request.content not available
using (Stream fileStream = await provider.Contents[0].ReadAsStreamAsync())
part = provider.Contents[0].Headers.GetData(); //public static FilePart GetData name,length,id
part.Data = fileStream.ReadFully();
*/
后端我正在工作,但找不到新的 asp.net 核心控制器解析文件对象和数据从客户端发布到服务的方法!任何想法都会像往常一样不胜感激......
【问题讨论】:
您将ByteArrayContent
传递给控制器,而不是MultipartFormDataContent
您确定吗?
在这行var result = client.PostAsync("/api/file", fileContent).Result;
嗨@Niladri是的这是一个错误,应该是 var result = client.PostAsync("/api/file", content).Result;不是文件内容
我已经更新了我的答案,请检查
【参考方案1】:
您可以修改您的控制器操作方法,如下所示,根据需要接受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;
【讨论】:
带有“UnsupportedMediaType”的 [FromBody] 属性客户端退出,似乎无法到达服务器端 POST。 @Lakmal 您使用的是哪个版本的 Web api? @Lakmal 你把它改成var result = client.PostAsync("/api/file", content).Result;
你的意思是“Microsoft.AspNet.WebApi.Client”:“5.2.3”,“Microsoft.AspNetCore.Mvc”:“1.0.1”?
是的,var result = client.PostAsync("/api/file", content).Result;【参考方案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");
【讨论】:
以上是关于如何在 ASP.NET Core Web API 控制器中接收字节数组和标头内容的主要内容,如果未能解决你的问题,请参考以下文章