Implement file upload
This commit is contained in:
33
Hcs.WebApp/Controllers/UploadController.cs
Normal file
33
Hcs.WebApp/Controllers/UploadController.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Hcs.WebApp.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[DisableRequestSizeLimit]
|
||||
public class UploadController(IWebHostEnvironment environment) : Controller
|
||||
{
|
||||
private readonly IWebHostEnvironment environment = environment;
|
||||
|
||||
[HttpPost("upload/parsing")]
|
||||
public IActionResult Single(IFormFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileName = $"{DateTime.Today:dd-MM-yyyy}-{Guid.NewGuid()}{Path.GetExtension(file.FileName)}";
|
||||
using var stream = new FileStream(Path.Combine(environment.WebRootPath, fileName), FileMode.Create);
|
||||
file.CopyTo(stream);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
Path = Url.Content($"~/{fileName}"),
|
||||
FileName = file.FileName
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user