Mercurial > Grille > Grille-Config
diff Controllers/UploadController.cs @ 0:689cde763395
init cimmit
author | Franklin Schmit <meokcin@gmail.com> |
---|---|
date | Thu, 05 Sep 2024 10:16:16 +0800 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Controllers/UploadController.cs Thu Sep 05 10:16:16 2024 +0800 @@ -0,0 +1,88 @@ +using System; +using System.IO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Hosting; + +namespace Grille2.Controllers +{ + public partial class UploadController : Controller + { + private readonly IWebHostEnvironment environment; + + public UploadController(IWebHostEnvironment environment) + { + this.environment = environment; + } + + // Single file upload + [HttpPost("upload/single")] + public IActionResult Single(IFormFile file) + { + try + { + // Put your code here + return StatusCode(200); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + // Multiple files upload + [HttpPost("upload/multiple")] + public IActionResult Multiple(IFormFile[] files) + { + try + { + // Put your code here + return StatusCode(200); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + // Multiple files upload with parameter + [HttpPost("upload/{id}")] + public IActionResult Post(IFormFile[] files, int id) + { + try + { + // Put your code here + return StatusCode(200); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + // Image file upload (used by HtmlEditor components) + [HttpPost("upload/image")] + public IActionResult Image(IFormFile file) + { + try + { + var fileName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}"; + + using (var stream = new FileStream(Path.Combine(environment.WebRootPath, fileName), FileMode.Create)) + { + // Save the file + file.CopyTo(stream); + + // Return the URL of the file + var url = Url.Content($"~/{fileName}"); + + return Ok(new { Url = url }); + } + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + } +}