zoukankan      html  css  js  c++  java
  • Flask Web学习笔记(六)

    Flask博客开启CKeditor的图片上传功能


    1.开启CKeditor的本地浏览按钮

    先在Flask项目的static文件夹里创建browser文件夹,再在browser文件夹里创建browse.html

    接着在CKeditor的文件夹里找到config.js,在里面配置

    config.filebrowserBrowseUrl = '/static/browser/browse.html';

    2.在browse.html里编写CKeditor的API接口

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>浏览本地文件</title>
        
        <script>
            // Helper function to get parameters from the query string.
            function getUrlParam( paramName ) {
                var reParam = new RegExp( '(?:[?&]|&)' + paramName + '=([^&]+)', 'i' );
                var match = window.location.search.match( reParam );
    
                return ( match && match.length > 1 ) ? match[1] : null;
            }
            // Simulate user action of selecting a file to be returned to CKEditor.
            function returnFileUrl(){
                str = new Array();
                filename = form1.file.value;
                str = filename.split("\");
                extname = str[str.length - 1];
                fileUrl = '/static/upload/' + extname;
                var funcNum = getUrlParam( 'CKEditorFuncNum' );
                window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl, function() {
                    // Get the reference to a dialog window.
                    var dialog = this.getDialog();
                    // Check if this is the Image Properties dialog window.
                    if ( dialog.getName() == 'image' ) {
                        // Get the reference to a text field that stores the "alt" attribute.
                        var element = dialog.getContentElement( 'info', 'txtAlt' );
                        // Assign the new value.
                        if ( element )
                            element.setValue( 'alt text' );
                    }
                    // Return "false" to stop further execution. In such case CKEditor will ignore the second argument ("fileUrl")
                    // and the "onSelect" function assigned to the button that called the file manager (if defined).
                    // return false;
                } );
                window.close();
    }
        </script>
    </head>
    <body>
    <form enctype="multipart/form-data" name="form1">
     <input type="file" name="file" value="选择文件">
     <input type="button" value="确定" onClick="returnFileUrl()">
    </form>
    </body>
    </html> 

    3.打开localhost:5000,找到你的CKeditor打开图片上传,会发现多了一个浏览器服务按钮

     

    4.点击确定按钮就可以上传了

    经过3天的摸索,终于摸透了ckeditor上传图片。原创文章,码字不易,转载请标明出处。

  • 相关阅读:
    Docker大行其道—镜像
    Google 推出全新的两步验证机制
    安全警告——“Windows已经阻止此软件因为无法验证发行者”解决办法
    Word2010 自动生成二级编号
    正则表达式判断汉字
    c# IList<T> 深拷贝
    C#IList 取区间数据
    Oracle 同环比排除分母0
    Git 删除服务器的远程分支
    【转】C#获取当前路径7种方法
  • 原文地址:https://www.cnblogs.com/alanlee1473/p/10114311.html
Copyright © 2011-2022 走看看