111
This commit is contained in:
		
							parent
							
								
									26a87383a4
								
							
						
					
					
						commit
						86e73f4ef6
					
				| 
						 | 
				
			
			@ -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("上传失败");
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -60,6 +60,11 @@ namespace LY.App.Model
 | 
			
		|||
        /// </summary>
 | 
			
		||||
        [SugarColumn(ColumnName = "alt", ColumnDescription = "高度", IsNullable = true)]
 | 
			
		||||
        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>
 | 
			
		||||
        public double Lat { get; set; }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 图片
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public string Img { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
    public class UpdateDevice : AddDevice
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										16
									
								
								Program.cs
								
								
								
								
							
							
						
						
									
										16
									
								
								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<SqlSugarClient>(sp =>
 | 
			
		|||
        };
 | 
			
		||||
        //创建数据库和表的语句仅执行一次 
 | 
			
		||||
        //db.DbMaintenance.CreateDatabase();
 | 
			
		||||
        db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity));
 | 
			
		||||
      //  db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity));
 | 
			
		||||
#endif
 | 
			
		||||
        //过滤器写在这儿就行了
 | 
			
		||||
        // 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");
 | 
			
		||||
var app = builder.Build();
 | 
			
		||||
ServiceLocator.Instance = app.Services;
 | 
			
		||||
var device = app.Services.GetService<DeviceService>();
 | 
			
		||||
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())
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue