如何修复采用 IFormFile 的 ASP.NET Core Web API 中的 System.AggregateException?
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论