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:
As the title suggests, I am trying to upload an image using the
IFormFileIFormFile 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.This is the only code I have added to a default ASP .NET Web API project:
Controller:
[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}");
}}}[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}");
}}}