Using IFormFile in Web API project results in network failure.
Hello everyone reading this thread,
As the title suggests, I am trying to upload an image using the
This is the only code I have added to a default ASP .NET Web API project:
Controller:
IFormFile
interface in a property. When I upload the image, the system 'crashes' and I get a network failure with the following message:
Undocumented
Failed to fetch.
Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.
Undocumented
Failed to fetch.
Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.
c#
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpPost("simplepost")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> Create(IFormFile file)
{
Debug.WriteLine(file.FileName);
return Ok(file.FileName);
}
[HttpPost("uploadfile")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> UploadFile([FromForm] DtoImage model)
{
try
{
_logger.LogInformation("UploadFile called.");
if (model.File == null || model.File.Length == 0)
{
_logger.LogWarning("No file uploaded.");
return BadRequest("No file uploaded.");
}
if (string.IsNullOrEmpty(model.Name))
{
_logger.LogWarning("No name provided.");
return BadRequest("No name provided.");
}
var uploadsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
_logger.LogInformation("Uploads directory: {UploadsDirectory}", uploadsDirectory);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "Internal server error.");
return StatusCode(500, $"Internal server error: {ex.Message}");
}}}
c#
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpPost("simplepost")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> Create(IFormFile file)
{
Debug.WriteLine(file.FileName);
return Ok(file.FileName);
}
[HttpPost("uploadfile")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> UploadFile([FromForm] DtoImage model)
{
try
{
_logger.LogInformation("UploadFile called.");
if (model.File == null || model.File.Length == 0)
{
_logger.LogWarning("No file uploaded.");
return BadRequest("No file uploaded.");
}
if (string.IsNullOrEmpty(model.Name))
{
_logger.LogWarning("No name provided.");
return BadRequest("No name provided.");
}
var uploadsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
_logger.LogInformation("Uploads directory: {UploadsDirectory}", uploadsDirectory);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "Internal server error.");
return StatusCode(500, $"Internal server error: {ex.Message}");
}}}
1 Reply
c#
public class DtoImage
{
public string Name { get; set; }
public IFormFile File { get; set; }
}
c#
public class DtoImage
{
public string Name { get; set; }
public IFormFile File { get; set; }
}
D:\ImageUpload\bin\Debug\net8.0\ImageUpload.exe (process 16644) exited with code -1.
Program.cs (to enable double post in single controller):
c#
builder.Services.AddSwaggerGen(options =>
{
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
c#
builder.Services.AddSwaggerGen(options =>
{
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
c#
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>
c#
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>