简体   繁体   中英

Images upload to folder in Summernote editor

I have asp.net mvc 4 application. In that application I have form , in that form I have summernote rich text editor. But in this editor I have option to upload images to this editor.

But Once I fill rest of the fields and sumbit the this form. Its not saving images that I uploaded through summernote editor .

I want to upload images to following path ~/Content/Images/

and keep the path as ~/Content/Images/Image_Name.JPG in database field

So I tried to create my solution as following by following this solution and GitHub Issue reported here

from Viewpage

<div class="form-group">
 @Html.TextAreaFor(m => m.Field_Value, new { @class = "form-control summernote"})
</div>

Controller method

    [HttpPost]
    public byte[] UploadImage(HttpPostedFileBase file)
    {
        Stream fileStream = file.InputStream;
        var mStreamer = new MemoryStream();
        mStreamer.SetLength(fileStream.Length);
        fileStream.Read(mStreamer.GetBuffer(), 0, (int)fileStream.Length);
        mStreamer.Seek(0, SeekOrigin.Begin);
        byte[] fileBytes = mStreamer.GetBuffer();
        Stream stream = new MemoryStream(fileBytes);

        //string result = System.Text.Encoding.UTF8.GetString(fileBytes);
        var img = Bitmap.FromStream(stream);
        Directory.CreateDirectory("~//Content//Images//" + img);
        return fileBytes;

    }

this is script

<script type="text/javascript">
$('.summernote').summernote({
    height: 200,   // set editable area's height
    focus: true,   // set focus editable area after Initialize summernote
    onImageUpload: function (files, editor, welEditable) {
        var formData = new FormData();
        formData.append("file", files[0]);

        $.ajax({
            url: "@Url.Action("Home", "UploadImage")",
            data: formData,
            type: 'POST',
            cache: false,
            contentType: false,
            processData: false,
            success: function (imageUrl) {
                if (!imageUrl) {
                    debugger;
                    // handle error
                    return;
                }
                editor.insertImage($editable, imageUrl);
            },
            error: function () {
                // handle error
            }
        }); 
    }
});

this once also, its not showing whether image uploaded to editor at all

I updated your script, and it works for me (check the changes on code comments):

<script type="text/javascript">
var editor =  // 1st change: will need this variable later
$('.summernote').summernote({
    height: 200,   
    focus: true,   
    callbacks: { // 2nd change - onImageUpload inside of "callbacks"
      onImageUpload: function (files) { // 3rd change - dont need other params
        var formData = new FormData();
        formData.append("file", files[0]);

        $.ajax({
            url: "@Url.Action("Home", "UploadImage")",
            data: formData,
            type: 'POST',
            cache: false,
            contentType: false,
            processData: false,
            success: function (imageUrl) {
                if (!imageUrl) {
                    // handle error
                    return;
                }
                // 4th change - create img element and add to document
                var imgNode = document.createElement('img');
                imgNode.src = imageUrl;
                editor.summernote('insertNode', imgNode);
            },
            error: function () {
                // handle error
            }
        });
      } 
    }
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM