本文介绍了无法使用 C# 客户端将 IFormFile 发送到 ASP.Net Core Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.Net Core Web API,其控制器 POST 方法定义如下:

I have an ASP.Net Core Web API, with a controller POST method defined like this:

[HttpPost("SubmitFile")]
public async Task<IActionResult> SubmitFile(IFormFile file)
{
}

我有一个客户端方法来调用 API SubmitFile() 方法,定义如下:

I have a client-side method to call the API SubmitFile() method, defined like this:

[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
     using (var client = new HttpClient())
     {
         client.BaseAddress = new Uri(_options.SiteSpecificUrl);

         foreach (var file in files)
         {
             if (file.Length <= 0)
                 continue;

             var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
             var fileContent = new StreamContent(file.OpenReadStream());
             fileContent.Headers.Add("X-FileName", fileName);
             fileContent.Headers.Add("X-ContentType", file.ContentType);

             var response = await client.PostAsync(_options.WebApiPortionOfUrl, fileContent);
         }
     }

    return View();
}

执行客户端发送时,在服务器端,SubmitFile() 中的断点显示文件参数为空.如何正确发送文件?保留服务器端 API 很重要,因为我让 Swashbuckle/Swagger 正确生成了可以发送文件的 UI.

When the client send is performed, on the server side a break-point in SubmitFile() shows that the file argument is null. How can I correctly send the file? It is important to preserve the server-side API, as I have Swashbuckle/Swagger correctly generating a UI that can send the file.

推荐答案

我找到了几种解决方案.这里是最简单的.请注意,这是一个 ASP.Net Core 客户端解决方案:

I've found a couple ways of doing this. Here is the simplest. Note that this is an ASP.Net Core client-side solution:

[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(_options.SiteSpecificUrl);

        foreach (var file in files)
        {
            if (file.Length <= 0)
                continue;

            var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

            using (var content = new MultipartFormDataContent())
            {
                content.Add(new StreamContent(file.OpenReadStream())
                {
                    Headers =
                    {
                        ContentLength = file.Length,
                        ContentType = new MediaTypeHeaderValue(file.ContentType)
                    }
                }, "File", fileName);

                var response = await client.PostAsync(_options.WebApiPortionOfUrl, content);
            }
        }
    }
}

这个控制器方法是从 .cshtml 页面调用的,如下所示:

This controller method is called from a .cshtml page as follows:

@{
    ViewData["Title"] = "Home Page";
}

<form method="post" asp-action="Index" asp-controller="Home" enctype="multipart/form-data">
    <input type="file" name="files" multiple />
    <input type="submit" value="Upload" />
</form>

此表单显示两个按钮,选择文件",显示选择文件"对话框,以及上传",调用 HomeController.Index 方法.

This form displays two buttons, "Choose Files", which presents a "select files" dialog, and "Upload", which calls the HomeController.Index method.

这篇关于无法使用 C# 客户端将 IFormFile 发送到 ASP.Net Core Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:00