如何修复采用 IFormFile 的 ASP.NET Core Web API 中的 System.AggregateException?

发布于 2025-01-10 10:44:41 字数 1941 浏览 0 评论 0原文

我正在创建一个 ASP.NET Core Web API,它将获取图像并将其存储在后端文件夹中,并将路径发送到 db。但它反复出现以下错误:

System.AggregateException:发生一个或多个错误。 (由于连接方在一段时间后没有正确响应而导致连接尝试失败,或者由于连接的主机未能响应而建立连接失败。)

下面是 API 操作方法:

[HttpPost, DisableRequestSizeLimit]
[Route("GetAll")]
public IActionResult GetAll()
{
        try
        {
            var file = Request.Form.Files[0];
            var folderName = Path.Combine("Auxiliary","Images");
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

            if (file.Length > 0)
            {
                var fileName=ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                var fullPath = Path.Combine(pathToSave, fileName);
                var dbPath = Path.Combine(folderName, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }

                return Ok(new {dbPath});
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal Server Error: {ex}");
        }
}

startup.cs 文件中,我在 ConfigureServices 方法中添加了以下代码:

services.Configure<FormOptions>(o =>
        {
            o.ValueLengthLimit = int.MaxValue;
            o.MultipartBodyLengthLimit = int.MaxValue;
            o.MemoryBufferThreshold = int.MaxValue;
        });

Configure 方法中:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Auxiliary")),
            RequestPath = new Microsoft.AspNetCore.Http.PathString("/Auxiliary")
        });

I am creating an ASP.NET Core Web API which will get image and store it in the a backend folder and send the path to db. But it's giving the following error repeatedly:

System.AggregateException: One or more errors occurred. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

Below is the API action method:

[HttpPost, DisableRequestSizeLimit]
[Route("GetAll")]
public IActionResult GetAll()
{
        try
        {
            var file = Request.Form.Files[0];
            var folderName = Path.Combine("Auxiliary","Images");
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

            if (file.Length > 0)
            {
                var fileName=ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                var fullPath = Path.Combine(pathToSave, fileName);
                var dbPath = Path.Combine(folderName, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }

                return Ok(new {dbPath});
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, 
quot;Internal Server Error: {ex}");
        }
}

And in startup.cs file, I have added below code in the ConfigureServices method:

services.Configure<FormOptions>(o =>
        {
            o.ValueLengthLimit = int.MaxValue;
            o.MultipartBodyLengthLimit = int.MaxValue;
            o.MemoryBufferThreshold = int.MaxValue;
        });

And in thew Configure method:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Auxiliary")),
            RequestPath = new Microsoft.AspNetCore.Http.PathString("/Auxiliary")
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文