phoben 发表于 2023-11-1 12:41:50

HTTP请求中处理文件

本帖最后由 phoben 于 2023-11-1 12:48 编辑

场景:与第三方系统对接时,无论是发送POST请求还是接收别人的请求,都存在需要接收文件的情况。

需求:
    1. 发送POST请求时,body中允许同时携带普通数据和文件数据。
      在C#中,实际上就是允许定义StringContent和fileContent

    2. 服务端命令中可以接收一个文件类型的参数,并可在命令中获取这个文件保存到本地,形成一个文件名
      在C#中,实际上就是将接收的文件数据的FileStream读取到本地,然后根据用户指定的文件名保存到Upload文件夹中,并返回文件名。

总结:无论是发送请求还是接收别人的请求,都能对文件做出响应处理,下方是发送请求和接收请求的伪代码例子。

发送POST请求
using System;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
      using (var client = new HttpClient())
      {
            // 设置服务器端接收请求的URL
            var url = "你的服务器地址";

            // 创建MultipartFormDataContent
            using (var content = new MultipartFormDataContent())
            {
                // 添加字符串参数
                content.Add(new StringContent("your_username"), "username");
                content.Add(new StringContent("your_filesname"), "filesname");

                // 添加文件
                var filePaths = new[] { "file1.pdf", "file2.jpg", "file3.png", "file4.xlsx", "file5.docx" };
                foreach (var filePath in filePaths)
                {
                  var fileInfo = new FileInfo(filePath);
                  var fileStream = new FileStream(filePath, FileMode.Open);
                  var fileContent = new StreamContent(fileStream);
                  content.Add(fileContent, "files", fileInfo.Name);
                }

                // 发送POST请求
                var response = await client.PostAsync(url, content);

                // 读取服务器端返回的结果
                var responseContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseContent);
            }
      }
    }
}


接收请求
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;

")]

public class FileUploadController : ControllerBase
{
   
    public async Task<IActionResult> Upload(IFormCollection form)
    {
      try
      {
            // 获取参数
            var username = form["username"];
            var filesname = form["filesname"];

            // 打印参数
            Console.WriteLine($"Username: {username}");
            Console.WriteLine($"Filesname: {filesname}");

            // 获取文件
            var files = form.Files;
            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                  // 设置保存文件的路径
                  var filePath = Path.Combine("上传文件的保存路径", file.FileName);

                  // 创建文件流并保存文件
                  using (var stream = new FileStream(filePath, FileMode.Create))
                  {
                        await file.CopyToAsync(stream);
                  }

                  Console.WriteLine($"File {file.FileName} uploaded successfully!");
                }
            }

            return Ok("Files uploaded successfully");
      }
      catch (Exception ex)
      {
            return StatusCode(500, $"Internal server error: {ex}");
      }
    }
}




David.Zhong 发表于 2023-11-1 14:04:24

发送http请求命令支持文件这个已经记过需求啦,需求编号:42371。

关于接收文件类型返回值,可以用发送http请求命令,请求数据,再用保存内容到文件命令保存,
页: [1]
查看完整版本: HTTP请求中处理文件