Skip to content

Instantly share code, notes, and snippets.

@alexisselorm
Last active December 26, 2023 01:44
Show Gist options
  • Save alexisselorm/d5fa74667fe74f1ce3542a335227d0f8 to your computer and use it in GitHub Desktop.
Save alexisselorm/d5fa74667fe74f1ce3542a335227d0f8 to your computer and use it in GitHub Desktop.
Image controller
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace ckedior_image_upload_angular_net.Server.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ImageController : ControllerBase
{
private readonly ILogger<ImageController> _logger;
private readonly IConfiguration _configuration;
public ImageController(ILogger<ImageController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
// POST: api/Image
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult> Upload([FromForm] IFormFile upload)
{
try
{
_logger.LogInformation(JsonSerializer.Serialize(upload));
//If nothing was sent to the backend or the file was corrupted
if (upload == null || upload.Length == 0)
return BadRequest("Invalid image file");
//generate a unique file name
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);
string filePath = Path.Combine("wwwroot/images/", fileName);
//Save image file to assets
await using var fileStream = new FileStream(filePath, FileMode.Create);
await upload.CopyToAsync(fileStream);
//Image url path.
string path = $"images/{fileName}";
//Create a url generator helper function.
string ImageUrl = Urlify(_configuration, path);
//Return the URL to CKEditor to display
return CreatedAtAction(null, new { url = ImageUrl });
}
catch (Exception ex)
{
// Handle any exceptions
return BadRequest(ex.Message);
}
}
private string Urlify(IConfiguration configuration, string path)
{
return $"{configuration["AppUrl"]}/{path}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment