如何为Summernote的插入图像对话框设置图像扩展名

时间:2019-02-27 00:30:35

标签: jscript summernote

我正在使用Summernote,并且一切正常,但是我无法确定一件事。以下是我用于SummerNote的设置:

        summerNoteElement.summernote({
        toolbar: [
            ['magic', ['style', 'h1', 'h2', 'h3', 'h4']],
            ['actions', ['undo', 'redo']],
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['fontname', 'strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['media', ['picture', 'video', 'link', 'table', 'hr']],
            ['insert', ['map', 'localmap']],
            ['uploadcare', ['uploadcare']],
            ['misc', ['help', 'fullscreen']] 
        ],
        height: 300,
        maximumImageFileSize: maxImageFileSize,
    }

现在,我在此处附加屏幕截图以显示我要更改的内容。因此,我点击下面栏中的插入图片按钮: enter image description here

然后单击“选择文件”: enter image description here

现在,如果您选择文件扩展名,您将看到允许的图像格式列表: enter image description here

我想做的是将这些扩展名限制为仅JPG,BMP,PNG之类的一些扩展名。

我是否缺少任何配置或执行此操作的技巧?谢谢。

1 个答案:

答案 0 :(得分:1)

summernote没有此选项来控制扩展,它可以接受所有类型的图像,但是我需要释放以pdf格式上传的文件并找到解决方案检查:

在summernote.js的主文件中查找以下行:

'type = "file" name = "files" accept = "image / *" multiple = "multiple" />' +

,然后将accept =“ image / *”更改为所需的格式 例如:

' type="file" name="files" accept="image/gif,image/jpeg,image/jpg,image/png,application/pdf,application/vnd.ms-excel,application/msword, application/vnd.ms-powerpoint,text/plain" multiple="multiple" />' +

希望这会有所帮助

对于那些需要完整代码来发送图像的人,以下还包括服务器通过summernote发送的pdf文件,doc,txt等:

Js代码

$ (document) .ready (function () {

        (summernote) summernote ({
           toolbar: [
            ['myotherbutton', ['parameters']],
            ['style', ['style']],
            ['font', ['bold', 'italic', 'underline', 'clear'
            ['fontname', ['fontname']],
['fontsize', ['fontsize']],
            ['color', ['color']]
            ['for', ['ul', 'ol', 'paragraph']]
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video', 'hr', 'codeview']]
],
lang: 'pt-BR',
height: 300,
minHeight: null,
maxHeight: null,
            callbacks: {
                onImageUpload: function (files) {
                    uploadFile (files [0]);
                },
onMediaDelete: function (target) {
                alert (target [0] .src)
                deleteFile (target [0] .src);
}
            }
        });

    function uploadFile (file) {
       
   data = new FormData ();
        data.append ("file", file);

        $ .ajax ({
            date: date,
            type: "POST",
            url: "../midia/upload.php", // replace with your url
            cache: false,
            contentType: false,
            processData: false,
            success: function (url) {

alert (url);

function checkURL (url) {
return (url.match (/ \. (jpeg | jpg | gif | png) $ /)! = null);
}

if (checkURL (url)) {

                var linkfile = url.split ("Images /");

alert (linkfile [1]);

$ image = "../midia/Images/"+linkfile[1];

$ ('# publisher'). summernote ("insertImage", $ image);

} else {

var linkfile = url.split ("Images /");

$ ('# publisher'). summernote ('createLink', {
text: linkfile [1],
url: url,
isNewWindow: true
});

}

            }
        });
    }

function deleteFile (src) {

    $ .ajax ({
        date: {src: src},
        type: "POST",
        url: "../midia/delete.php", // replace with your url
        cache: false,
        success: function (resp) {
            console.log (resp);
        }
    });
}


});

将php文件发送到文件夹/媒体/

upload.php

<? php
      $ allowed = array ('png', 'jpg', 'gif', 'pdf', 'doc', 'docx', 'txt', 'mp3');
        if (isset ($ _ FILES ['file']) && $ _FILES ['file'] ['error'] == 0) {
            $ extension = pathinfo ($ _FILES ['file'] ['name'], PATHINFO_EXTENSION);
            if (! in_array (strtolower ($ extension), $ allowed)) {
                 echo 'AN ERROR OCCURED - INVALID IMAGE';
                exit;
            }
            if (move_uploaded_file ($ _FILES ['file'] ['tmp_name'], '../midia/Images/'.$_FILES['file']['name'])) {
                echo $ _SERVER ['SERVER_NAME']. dirname ($ _ SERVER ['PHP_SELF']). '/ Images /'.$_ FILES [' file '] [' name ']; // echo absolute file_url
                exit;
            }
        }
        echo 'AN ERROR OCCURED';
        exit;
?>

delete.php

<? php

$ url = explode ("Images", $ _ POST ['src']);

        $ file_name = str_replace ($ url [0], '', $ _ POST ['src']); // striping host to get relative path

        if (unlink ($ file_name))
        {
            echo 'file deleted!';
        }
?>