mirror of
https://github.com/hargata/lubelog.git
synced 2025-12-12 00:03:10 -06:00
48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using CarCareTracker.External.Interfaces;
|
|
using CarCareTracker.Models;
|
|
using LiteDB;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Diagnostics;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using System.Drawing;
|
|
using System.Linq.Expressions;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CarCareTracker.Controllers
|
|
{
|
|
public class FilesController : Controller
|
|
{
|
|
private readonly ILogger<FilesController> _logger;
|
|
private readonly IVehicleDataAccess _dataAccess;
|
|
private readonly IWebHostEnvironment _webEnv;
|
|
|
|
public FilesController(ILogger<FilesController> logger, IWebHostEnvironment webEnv)
|
|
{
|
|
_logger = logger;
|
|
_webEnv = webEnv;
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult HandleFileUpload(IFormFile file)
|
|
{
|
|
var fileName = UploadImage(file);
|
|
return Json(fileName);
|
|
}
|
|
|
|
private string UploadImage(IFormFile fileToUpload)
|
|
{
|
|
string uploadDirectory = "images/";
|
|
string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory);
|
|
if (!Directory.Exists(uploadPath))
|
|
Directory.CreateDirectory(uploadPath);
|
|
string fileName = Guid.NewGuid() + Path.GetExtension(fileToUpload.FileName);
|
|
string filePath = Path.Combine(uploadPath, fileName);
|
|
using (var stream = System.IO.File.Create(filePath))
|
|
{
|
|
fileToUpload.CopyTo(stream);
|
|
}
|
|
return Path.Combine("/", uploadDirectory, fileName);
|
|
}
|
|
}
|
|
}
|