将文件从ASP.NET Core Web API发布到另一个ASP.NET Core Web API [英] Post files from ASP.NET Core web api to another ASP.NET Core web api

查看:113
本文介绍了将文件从ASP.NET Core Web API发布到另一个ASP.NET Core Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个由Angular2前端,一个ASP.NET Core Web api公共后端和一个ASP.NET Core Web api私有后端组成的Web应用程序.

We are building a web application that consist of an Angular2 frontend, a ASP.NET Core web api public backend, and a ASP.NET Core web api private backend.

将文件从Angular2上载到公共后端.但是我们希望将它们转发到私有后端.

Uploading files from Angular2 to the public backend works. But we would prefer to post them forward to the private backend.

当前工作代码

[HttpPost]
public StatusCodeResult Post(IFormFile file)
{
  ...
}

从那里,我可以使用file.CopyTo(fileStream);将文件保存到磁盘上.

From there I can save the file to disk using file.CopyTo(fileStream);

但是,我想将一个或多个文件或整个请求重新发送到第二个Web api核心.

However, I want to re-send that file, or those files, or, ideally, the whole request to my second web api core.

我不确定如何使用asp.net核心的HttpClient类实现此目标.

I am not sure how to achieve this with the HttpClient class of asp.net core.

我尝试过各种事情,例如

I've tried all kinds of things such as

StreamContent ss = new StreamContent(HttpContext.Request.Body);
var result = client.PostAsync("api/Values", ss).Result;

但是我的第二个后端有一个空的IFormFile.

But my second backend gets an empty IFormFile.

我觉得可以将文件作为流发送并在另一侧重建它们,但无法使其正常工作.

I have a feeling it is possible to send the file(s) as a stream and reconstruct them on the other side, but can't get it to work.

该解决方案必须使用两个Web api核心.

The solution must use two web api core.

推荐答案

解决方案

DMZ中的公共后端

Solution

Public backend in DMZ

[HttpPost]
public StatusCodeResult Post(IFormFile file)
{
    try
    {
        if (file != null && file.Length > 0)
        {
            using (var client = new HttpClient())
            {
                try
                {
                    client.BaseAddress = new Uri(currentPrivateBackendAddress);
                    
                    byte[] data;
                    using (var br = new BinaryReader(file.OpenReadStream()))
                        data = br.ReadBytes((int)file.OpenReadStream().Length);

                    ByteArrayContent bytes = new ByteArrayContent(data);

                    
                    MultipartFormDataContent multiContent = new MultipartFormDataContent();
                    
                    multiContent.Add(bytes, "file", file.FileName);

                    var result = client.PostAsync("api/Values", multiContent).Result;
                    

                    return StatusCode((int)result.StatusCode); //201 Created the request has been fulfilled, resulting in the creation of a new resource.
                                                
                }
                catch (Exception)
                {
                    return StatusCode(500); // 500 is generic server error
                }
            }
        }

        return StatusCode(400); // 400 is bad request

    }
    catch (Exception)
    {
        return StatusCode(500); // 500 is generic server error
    }
}

私人后端

[HttpPost]
public void Post()
{
    //Stream bodyStream = HttpContext.Request.Body;

    if (Request.HasFormContentType)
    {
        var form = Request.Form;
        foreach (var formFile in form.Files)
        {
            var targetDirectory = Path.Combine(_appEnvironment.WebRootPath, "uploads");

            var fileName = GetFileName(formFile);

            var savePath = Path.Combine(targetDirectory, fileName);

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

这篇关于将文件从ASP.NET Core Web API发布到另一个ASP.NET Core Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆