【发布时间】:2021-06-04 03:25:52
【问题描述】:
我正在尝试通过工作服务将文件数据分块发送到 Web api。但是 web api 无法接收块,这是我的代码
这是一些工人服务的代码
int chunkSize = 100;
using (Stream streamx = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[chunkSize];
int bytesRead = 0;
long bytesToRead = streamx.Length;
while (bytesToRead > 0)
{
int n = streamx.Read(buffer, 0, chunkSize);
if (n == 0) break;
// Let's resize the last incomplete buffer
if (n != buffer.Length)
Array.Resize(ref buffer, n);
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent byteContent = new ByteArrayContent(buffer);
form.Add(byteContent, "fileByte");
HttpResponseMessage response1 = null;
try
{
response1 = client.PostAsync("http://localhost:15594/weatherforecast/PostData", form).Result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
var k = response1.Content.ReadAsStringAsync().Result;
response = null;
bytesRead += n;
bytesToRead -= n;
}
这是一些web api的代码
[HttpPost]
[Route("PostData")]
public IActionResult PostData()
{
var request = HttpContext.Request;
var vals = request.Form.TryGetValue("fileByte", out StringValues sv).ToString();
NameValueCollection vals2 = new NameValueCollection();
int count = vals2.Count;
string cv = null;
foreach(var i in vals2)
{
cv += i;
}
}
【问题讨论】:
-
您能否详细说明“web api 无法接收块”?我不知道你具体是什么意思。
-
PostDataimpl。毫无意义。您如何期望vals2包含任何数据? -
这里我将 httpcontent 数据发送到 web api,但在 weapi 中它接收到 null。任何人都可以给出任何想法将httpcontent发送到webapi以及如何通过webapi接收httpcontent
标签: c# asp.net-web-api .net-core