如何从ckeditor上传服务器中的图像

时间:2019-08-18 20:47:38

标签: asp.net-core-2.1 ckeditor5

此用于在服务器中保存图像的api:

[HttpPost]
public IActionResult Image(IFormFile formFile)
{
    try
    {
        var folderName = Path.Combine(_environment.WebRootPath, "upload");
        var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
        if (formFile.Length > 0)
        {
            var fileName = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
            var fullPath = Path.Combine(pathToSave, fileName);
            var dbPath = Path.Combine(folderName, fileName);
            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                    formFile.CopyTo(stream);
            }
            return new JsonResult(new { dbPath });
        }
        else
        {
            return BadRequest();
        }
   }
   catch (Exception ex)
   {
       return StatusCode(500, "Internal server error");
   }
}

这在客户端:

<script src="~/lib/ckeditor/ckeditor.js"></script>
<div class="form-group">
@Html.LabelFor(model => model.FullDescription, htmlAttributes: new { @class = "control-label col-md-2" })

<div ass="col-md-10">
  @Html.TextAreaFor(model => model.FullDescription, new { @id = "FullDescription", @class = "form-control", @rows = "200" }) @Html.ValidationMessageFor(model => model.FullDescription, "", new { @class = "text-danger" })
<script>
CKEDITOR.replace("FullDescription", {height:400,filebrowserImageUploadUrl:'/api/Image'});
<script>

并给我一个错误:ckeditor.js:817 POST https://localhost:44366/api/Image&responseType=json 404

1 个答案:

答案 0 :(得分:0)

我已经进行了快速搜索,您可以找到一个完全符合您想要的功能的插件,您可以在这里找到它:https://ckeditor.com/cke4/addon/uploadimage

对于一般上载图像,您应该查看以下代码:https://www.w3schools.com/php/php_file_upload.asp