【问题标题】:is there any way to get data from service to web api有没有办法从服务获取数据到 web api
【发布时间】: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 无法接收块”?我不知道你具体是什么意思。
  • PostData impl。毫无意义。您如何期望vals2 包含任何数据?
  • 这里我将 httpcontent 数据发送到 web api,但在 weapi 中它接收到 null。任何人都可以给出任何想法将httpcontent发送到webapi以及如何通过webapi接收httpcontent

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


【解决方案1】:

到这里我已经解决了问题,基本上我是想分块上传文件, 这里是文件上传到webapi的代码,

int chunkSize = 1024

        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;



                if (n != buffer.Length)
                    Array.Resize(ref buffer, n);



                MultipartFormDataContent multiContent = new MultipartFormDataContent();
                ByteArrayContent bytes = new ByteArrayContent(buffer);
                multiContent.Add(bytes, "file", fi.Name);




                HttpResponseMessage response1 = null;
                try
                {
                    response1 = client.PostAsync("http://localhost:15594/weatherforecast/PostData", multiContent).Result;



                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }



                var k = response1.Content.ReadAsStringAsync().Result;
                response = null;



                bytesRead += n;
                bytesToRead -= n;



            }
        }

''' 这是webapi的代码sn-p

'''

[HttpPost] [路线(“邮政数据”)]

    public IActionResult PostData()
    {​​​​​​​


        var form = Request.Form;
        foreach (var formFile in form.Files)
        {​​​​​​​


            var fileName = formFile.FileName;


            var savePath = Path.Combine(@"E:\Program\_210302_1WebApplication_API\_210302_1WebApplication_API\wwwroot", fileName);


            using (var fileStream = new FileStream(savePath, FileMode.Append))
            {​​​​​​​
                formFile.CopyTo(fileStream);
            }​​​​​​​
        }​​​​​​​

}​​​​​​​​​

'''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多