将文件从ASP.NET Core Web API发布到另一个ASP.NET Core Web API

编程入门 行业动态 更新时间:2024-10-18 23:23:57
本文介绍了将文件从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核心的HttpClient类实现此目标.

I am not sure how to achieve this with the HttpClient class of asp 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

本文发布于:2023-11-13 15:11:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   ASP   NET   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!