This commit is contained in:
yanghongwei 2025-03-24 11:26:11 +08:00
parent 26a87383a4
commit 86e73f4ef6
3 changed files with 89 additions and 2 deletions

View File

@ -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<IActionResult> 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("上传失败");
}
}
}

View File

@ -60,6 +60,11 @@ namespace LY.App.Model
/// </summary> /// </summary>
[SugarColumn(ColumnName = "alt", ColumnDescription = "高度", IsNullable = true)] [SugarColumn(ColumnName = "alt", ColumnDescription = "高度", IsNullable = true)]
public double Alt { get; set; } public double Alt { get; set; }
[SugarColumn(ColumnName = "img", ColumnDescription = "图片", IsNullable = true)]
/// <summary>
/// 图片
/// </summary>
public string Img { get; set; }
} }
@ -96,6 +101,10 @@ namespace LY.App.Model
/// 纬度 /// 纬度
/// </summary> /// </summary>
public double Lat { get; set; } public double Lat { get; set; }
/// <summary>
/// 图片
/// </summary>
public string Img { get; set; }
} }
public class UpdateDevice : AddDevice public class UpdateDevice : AddDevice
{ {

View File

@ -10,6 +10,7 @@ using LY.App.Service;
using LY.App.Model; using LY.App.Model;
using LY.App.MiddleWare; using LY.App.MiddleWare;
using LY.App.Common.WebSocket; using LY.App.Common.WebSocket;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -75,19 +76,30 @@ builder.Services.AddTransient<SqlSugarClient>(sp =>
}; };
//创建数据库和表的语句仅执行一次 //创建数据库和表的语句仅执行一次
//db.DbMaintenance.CreateDatabase(); //db.DbMaintenance.CreateDatabase();
db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity)); // db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity));
#endif #endif
//过滤器写在这儿就行了 //过滤器写在这儿就行了
// db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == false); // db.QueryFilter.AddTableFilter<IDeleted>(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"); SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowFlakeWordId")?.Value ?? "1");
var app = builder.Build(); var app = builder.Build();
ServiceLocator.Instance = app.Services; ServiceLocator.Instance = app.Services;
var device = app.Services.GetService<DeviceService>(); var device = app.Services.GetService<DeviceService>();
device?.Init(); device?.Init();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")),
RequestPath = "/upload"
});
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {