How to implement image upload to the PageDown markdown editor?
Asked Answered
S

1

9

To implement image upload to the PageDown markdown editor, I have modified some code about the editor.

Markdown.Editor.js

var defaultsStrings = { imagedialog : "< input id='image' type='file' />" }

When select a picture and then click ok button to sent ajax request. It can return image path.

var okButton = doc.createElement("input");
        okButton.type = "button";
        okButton.onclick = function () { 
            var data = new FormData();
            data.append('file', $( '#image' )[0].files[0] );
            $.ajax({
                url: 'uploadFile',
                data: data,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function ( data ) {
                    alert(path);
                }
            });
            return close(false);};

How to preview the image in the editor preview area?

Snowshoe answered 13/3, 2013 at 1:3 Comment(0)
S
6

This article provide a useful method,

editor.hooks.set("insertImageDialog", function (callback) {
 var $input = $('<input type="file" name="File" id="file_0" class = "fileUpload"/>');
 var $okButton = $('<a class="okButton">'+uploadOK()+'</a>');
 $okButton.click(function(){
        var data = new FormData();
        var file = $input[0].files[0];
       if (file === undefined || null === file) {
          // alert("error message);
       } else {
         data.append('file',  file);
        $.ajax({
            url: 'uploadFile',
            data: data,
            dataType : 'json',
            processData: false,
            contentType: false,
            type: 'POST',
            success: function ( data ) {
                  callback(data.dataObject.url);
            },
            error : function(data){
                  // error
            }
        });    
      }
    }); )
Snowshoe answered 3/9, 2013 at 6:0 Comment(8)
can you tell me where to include that please ?Nimocks
@AmitJoki Include what?Snowshoe
I want to include it in my project. Could you please provide me the full code ?Nimocks
I wrote the above code in another js file. And var converter2 = Markdown.getSanitizingConverter();var editor2 = new Markdown.Editor(converter2, '', options);Snowshoe
what's that converter2 ?Nimocks
I think that will throw an error like converter2 is undefinedNimocks
also in the last line instead of }); ) i think it should be })});Nimocks
@AmitJoki You can find more information hereSnowshoe

© 2022 - 2024 — McMap. All rights reserved.