layui 富文本编辑 图片上传 c#服务接口 实测可用
需依据自身需求适当修改,今天网上找了好半天没找到可用的c#服务接口,被逼自己琢磨了数据来源。
核心难点部分 :获取到layui上传的图片数据。
var files = Request.Form.Files;
/// <summary>
/// 上传图片
/// 网站上传接口
/// </summary>
/// <param name="image"></param>
[HttpPost]
public async Task<IActionResult> UploadImages()
{
string url =Request.Scheme+ "://" + Request.Host.ToString();
Result<MyClass> result = new Result<MyClass>();
try
{
MyClass info = new MyClass();
var files = Request.Form.Files; //获取选中文件
string centerPath = "/upload/" + DateTime.Now.ToString("yyyy-MM") + "/";
if (!Directory.Exists(_hostingEnvironment.WebRootPath + centerPath))
{
Directory.CreateDirectory(_hostingEnvironment.WebRootPath + centerPath);
}
foreach (var file in files)
{
var fileName = file.FileName;
var fileType = Path.GetExtension(file.FileName);
var newfileName = Guid.NewGuid().ToString("N") + fileType;
fileName = centerPath + newfileName;
var fileallPath = _hostingEnvironment.WebRootPath + fileName;
using (FileStream fs = System.IO.File.Create(fileallPath))
{
await file.CopyToAsync(fs);
fs.Flush();
}
info.src = url + fileName;
info.title = "图片标题";
}
result.code = ResultCode.Success;
result.msg = "图片上传成功";
result.data = info;
}
catch (Exception ex)
{
result.code = ResultCode.Failed;
result.msg = "图片上传成功";
}
return Json(result);
}
public class MyClass
{
//图片地址
public string src { get; set; }
//图片标题
public string title { get; set; }
}