diff --git a/Controllers/UploadController.cs b/Controllers/UploadController.cs new file mode 100644 index 0000000..780a9d1 --- /dev/null +++ b/Controllers/UploadController.cs @@ -0,0 +1,66 @@ +using LY.App.Model; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace LY.App.Controllers +{ + public class UploadController : Controller + { + + public static readonly string uploadPath = "upload"; + + [HttpPost("uploadImg")] + public async Task UploadImg() + { + ApiResult result = new ApiResult(); + result.code = 1; + if (!Request.HasFormContentType) + { + return BadRequest("未选择文件"); + } + + IFormFileCollection cols = Request.Form.Files; + if (cols.Count == 0) + return BadRequest("上传失败"); + const string fileFilt = ".gif|.jpg|.jpeg|.png|.bmp"; + foreach (IFormFile file in cols) + { + var fileExtension = Path.GetExtension(file.FileName); + + if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1) + { + result.msg = "上传的文件格式不正确"; + return Ok(result); + } + + long length = file.Length; + if (length > 1024 * 1024 * 5) + { + result.msg = "上传的文件不能大于5M"; + return Ok(result); + } + + var strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff"); + var strRan = Convert.ToString(new Random().Next(100, 999)); + var fileName = strDateTime + strRan + fileExtension; + string path = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Img")); + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + var filePath = Path.Combine(path, fileName); + + using (var stream = new FileStream(filePath, FileMode.Create)) + { + file.CopyTo(stream); + } + result.data = $"/{uploadPath}/{fileName}"; + result.code = 0; + return Ok(result); + } + + return BadRequest("上传失败"); + + } + } +} diff --git a/Model/DeviceEntity.cs b/Model/DeviceEntity.cs index 720e65d..6f04901 100644 --- a/Model/DeviceEntity.cs +++ b/Model/DeviceEntity.cs @@ -60,6 +60,11 @@ namespace LY.App.Model /// [SugarColumn(ColumnName = "alt", ColumnDescription = "高度", IsNullable = true)] public double Alt { get; set; } + [SugarColumn(ColumnName = "img", ColumnDescription = "图片", IsNullable = true)] + /// + /// 图片 + /// + public string Img { get; set; } } @@ -96,6 +101,10 @@ namespace LY.App.Model /// 纬度 /// public double Lat { get; set; } + /// + /// 图片 + /// + public string Img { get; set; } } public class UpdateDevice : AddDevice { diff --git a/Program.cs b/Program.cs index 5fa82c8..e93e2c1 100644 --- a/Program.cs +++ b/Program.cs @@ -10,6 +10,7 @@ using LY.App.Service; using LY.App.Model; using LY.App.MiddleWare; using LY.App.Common.WebSocket; +using Microsoft.Extensions.FileProviders; var builder = WebApplication.CreateBuilder(args); @@ -75,19 +76,30 @@ builder.Services.AddTransient(sp => }; //ݿͱִһ //db.DbMaintenance.CreateDatabase(); - db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity)); + // db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity)); #endif //д // db.QueryFilter.AddTableFilter(it => it.IsDeleted == false); }); }); +//ⲢͼƬ洢ļĿ¼ +string path = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Img")); +if (!Directory.Exists(path)) +{ + Directory.CreateDirectory(path); +} + SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowFlakeWordId")?.Value ?? "1"); var app = builder.Build(); ServiceLocator.Instance = app.Services; var device = app.Services.GetService(); device?.Init(); - +app.UseStaticFiles(new StaticFileOptions() +{ + FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")), + RequestPath = "/upload" +}); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {