欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Kindeditor(版本号4.0.5)编辑器添加上传flv视频功能

程序员文章站 2022-06-11 18:10:03
kindeditor编辑器是挺好用的,可是上传的flv视频文件在前台是无法访问的。到底是什么问题,先看看通过该编辑器上传的flv视频文件的源代码: [html] 

kindeditor编辑器是挺好用的,可是上传的flv视频文件在前台是无法访问的。到底是什么问题,先看看通过该编辑器上传的flv视频文件的源代码:
[html] 
<embed src="/upload/201210/22/201210221043235781.flv" type="application/x-shockwave-flash" width="550" height="400" quality="high" /> 
       分析一下上边的代码,这是播放swf文件的代码,而要在网页里边播放flv视频文件,需要加载一个播放器了,如果把上边代码改成如下代码:
[html] 
<embed src="/editor/plugins/flv/swf/flvplayer.swf?astr_file=/upload/201210/19/201210191854485625.flvtype="application/x-shockwave-flash" width="550" height="400" quality="high" /> 
       就可以在网页中正常访问了,分析一下这段代码中的src,就是访问flvplayer.swf播放器并通过该播放器加载相应的flv视频文件,这样才可以在网页中正常打开flv视频文件。下边就介绍如何在kindeditor编辑器中添加该功能。
1.       在\themes\common文件夹下新添加flv标志的图片flv.gif。
2.       修改\themes\default\default.png图片,在其底部添加flv图标。
3.       在编辑器功能菜单中显示添加flv文件的菜单,修改\themes\default\default.css文件,找到.ke-icon-flash样式,在其下边添加一段新的样式,代码如下:
[css] 
.ke-icon-flv { 
 
       background-position: 0px -1232px; 
 
       width: 16px; 
 
       height: 16px; 
 
}  

4.       修改lang文件夹下的zh_cn.js文件,找到
[javascript] 
flash: 'flash', 
并在其下边添加代码
[javascript] 
flv: 'flv', 
5.       plugins文件夹下新建文件夹flv,并在flv文件夹下再新添加一个文件夹swf,把播放flv文件的swf文件flvplayer.swf拷贝到该文件夹下,在flv文件夹里新添加flv.js文件。代码如下:
[javascript]
/*******************************************************************************
 
* kindeditor - wysiwyg html editor for internet
 
* copyright (c) 2006-2011 kindsoft.net
 
*
 
* @author juhnpen <>
 
* @site
 
* @licence
 
*******************************************************************************/ 
 
  
 
kindeditor.plugin('flv', function (k) { 
 
    var self = this, name = 'flv', lang = self.lang(name + '.'), 
 
              allowflvupload = k.undef(self.allowflvupload, true), 
 
              allowfilemanager = k.undef(self.allowfilemanager, false), 
 
              uploadjson = k.undef(self.uploadjson, self.basepath + 'php/upload_json.php'); 
 
    self.plugin.flv = { 
 
        edit: function () { 
 
            var html = [ 
 
                            '<p style="padding:10px 20px;">', 
 
            //url 
 
                            '<p class="ke-dialog-row">', 
 
                            '<label for="keurl" style="width:60px;">' + lang.url + '</label>', 
 
                            '<input class="ke-input-text" type="text" id="keurl" name="url" value="" style="width:160px;" />  ', 
 
                            '<input type="button" class="ke-upload-button" value="' + lang.upload + '" />  ', 
 
                            '<span class="ke-button-common ke-button-outer">', 
 
                            '<input type="button" class="ke-button-common ke-button" name="viewserver" value="' + lang.viewserver + '" />', 
 
                            '</span>', 
 
                            '</p>', 
 
            //width 
 
                            '<p class="ke-dialog-row">', 
 
                            '<label for="kewidth" style="width:60px;">' + lang.width + '</label>', 
 
                            '<input type="text" id="kewidth" class="ke-input-text ke-input-number" name="width" value="550" maxlength="4" /> ', 
 
                            '</p>', 
 
            //height 
 
                            '<p class="ke-dialog-row">', 
 
                            '<label for="keheight" style="width:60px;">' + lang.height + '</label>', 
 
                            '<input type="text" id="keheight" class="ke-input-text ke-input-number" name="height" value="400" maxlength="4" /> ', 
 
                            '</p>', 
 
                            '</p>' 
 
                     ].join(''); 
 
            var dialog = self.createdialog({ 
 
                name: name, 
 
                width: 450, 
 
                height: 200, 
 
                title: self.lang(name), 
 
                body: html, 
 
                yesbtn: { 
 
                    name: self.lang('yes'), 
 
                    click: function (e) { 
 
                        var url = k.trim(urlbox.val()), 
 
                                                 width = widthbox.val(), 
 
                                                 height = heightbox.val(); 
 
                        if (url == 'http://' || k.invalidurl(url)) { 
 
                            alert(self.lang('invalidurl')); 
 
                            urlbox[0].focus(); 
 
                            return; 
 
                        } 
 
                        if (!/^\d*$/.test(width)) { 
 
                            alert(self.lang('invalidwidth')); 
 
                            widthbox[0].focus(); 
 
                            return; 
 
                        } 
 
                        if (!/^\d*$/.test(height)) { 
 
                            alert(self.lang('invalidheight')); 
 
                            heightbox[0].focus(); 
 
                            return; 
 
                        } 
 
                        //删除修改flv是增加的str字符 
 
  
 
                        var str = '/editor/plugins/flv/swf/flvplayer.swf?vcastr_file='; 
 
                        if (url.indexof(str)>=0) { 
 
                            var last = url.substring(url.indexof(str) + str.length, url.length); 
 
                            url = last; 
 
                        } 
 
  
 
                        var html = k.mediaimg(self.themespath + 'common/blank.gif', { 
 
                            src: '/editor/plugins/flv/swf/flvplayer.swf?vcastr_file=' + url, 
 
                            type: 'application/x-shockwave-flash', 
 
                            width: width, 
 
                            height: height, 
 
                            quality: 'high', 
 
                            allowfullscreen: 'true' 
 
                        }); 
 
                        self.inserthtml(html).hidedialog().focus(); 
 
                    } 
 
                } 
 
            }), 
 
                     p = dialog.p, 
 
                     urlbox = k('[name="url"]', p), 
 
                     viewserverbtn = k('[name="viewserver"]', p), 
 
                     widthbox = k('[name="width"]', p), 
 
                     heightbox = k('[name="height"]', p); 
 
            urlbox.val('http://'); 
 
  
 
            if (allowflvupload) { 
 
                var uploadbutton = k.uploadbutton({ 
 
                    button: k('.ke-upload-button', p)[0], 
 
                    fieldname: 'imgfile', 
 
                    url: k.addparam(uploadjson, 'dir=flv'), 
 
                    afterupload: function (data) { 
 
                        dialog.hideloading(); 
 
                        if (data.error === 0) { 
 
                            var url = k.formaturl(data.url, 'absolute'); 
 
                            urlbox.val(url); 
 
                            if (self.afterupload) { 
 
                                self.afterupload.call(self, url); 
 
                            } 
 
                            alert(self.lang('uploadsuccess')); 
 
                        } else { 
 
                            alert(data.message); 
 
                        } 
 
                    }, 
 
                    aftererror: function (html) { 
 
                        dialog.hideloading(); 
 
                        self.errordialog(html); 
 
                    } 
 
                }); 
 
                uploadbutton.filebox.change(function (e) { 
 
                    dialog.showloading(self.lang('uploadloading')); 
 
                    uploadbutton.submit(); 
 
                }); 
 
            } else { 
 
                k('.ke-upload-button', p).hide(); 
 
                urlbox.width(250); 
 
            } 
 
  
 
            if (allowfilemanager) { 
 
                viewserverbtn.click(function (e) { 
 
                    self.loadplugin('filemanager', function () { 
 
                        self.plugin.filemanagerdialog({ 
 
                            viewtype: 'list', 
 
                            dirname: 'flv', 
 
                            clickfn: function (url, title) { 
 
                                if (self.dialogs.length > 1) { 
 
                                    k('[name="url"]', p).val(url); 
 
                                    self.hidedialog(); 
 
                                } 
 
                            } 
 
                        }); 
 
                    }); 
 
                }); 
 
            } else { 
 
                viewserverbtn.hide(); 
 
            } 
 
  
 
            var img = self.plugin.getselectedflv(); 
 
            if (img) { 
 
                var attrs = k.mediaattrs(img.attr('data-ke-tag')); 
 
                urlbox.val(attrs.src); 
 
                widthbox.val(k.removeunit(img.css('width')) || attrs.width || 0); 
 
                heightbox.val(k.removeunit(img.css('height')) || attrs.height || 0); 
 
            } 
 
            urlbox[0].focus(); 
 
            urlbox[0].select(); 
 
        }, 
 
        'delete': function () { 
 
            self.plugin.getselectedflv().remove(); 
 
        } 
 
    }; 
 
    self.clicktoolbar(name, self.plugin.flv.edit); 
 
}); 

6.       修改kindeditor.js文件,找到代码
[javascript] 
items: [ 
 
              'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste', 
 
              'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 
 
              'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 
 
              'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/', 
 
              'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 
 
              'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 
 
              'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'map', 'code', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' 

将其修改为
[javascript]
items: [ 
 
              'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste', 
 
              'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 
 
              'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 
 
              'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/', 
 
              'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 
 
              'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 
 
              'flash', 'flv', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'map', 'code', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' 

也就是在'flash',后边添加'flv',代码。
7.       修改kindeditor.js文件,找到function _mediatype(src)方法,将其里边的代码修改成下边代码:
 
[javascript] 
 
        if (/\.(rm|rmvb)(\?|$)/i.test(src)) { 
 
            return 'audio/x-pn-realaudio-plugin'; 
 
       } 
 
        if (/\.(flv)(\?|$)/i.test(src)) { 
 
            return 'application/x-shockwave-flv'; 
 
        } 
 
        if (/\.(swf)(\?|$)/i.test(src)) { 
 
            return 'application/x-shockwave-flash'; 
 
        } 
 
        return 'video/x-ms-asf-plugin'; 
 
    } 

8.       修改kindeditor.js文件,找到function _mediatype(src)方法,将其改为如下:
[javascript]
function _mediaclass(type) { 
 
        if (/realaudio/i.test(type)) { 
 
            return 'ke-rm'; 
 
        } 
 
        if (/flash/i.test(type)) { 
 
            return 'ke-flash'; 
 
        } 
 
        if (/flv/i.test(type)) { 
 
            return 'ke-flv'; 
 
        } 
 
        return 'ke-media'; 
 
    } 

9.       修改kindeditor.js文件,找到function _mediaimg(blankpath, attrs)方法,将其里边的type = attrs.type || _mediatype(attrs.src)改为type = _mediatype(attrs.src)
10.   修改kindeditor.js文件,找到_getinithtml(themespath, bodyclass, csspath, cssdata)方法,在代码
[javascript] 
 
              '      border:1px solid #aaa;', 
 
              '      background-image:url(' + themespath + 'common/flash.gif);', 
 
              '      background-position:center center;', 
 
              '      background-repeat:no-repeat;', 
 
              '      width:100px;', 
 
              '      height:100px;', 
 
              '}', 

   下边添加代码:
[javascript] 
'img.ke-flv {', 
 
              '      border:1px solid #aaa;', 
 
              '      background-image:url(' + themespath + 'common/flv.gif);', 
 
              '      background-position:center center;', 
 
              '      background-repeat:no-repeat;', 
 
              '      width:100px;', 
 
              '      height:100px;', 
 
              '}', 

11.   修改kindeditor.js文件,找到代码
[javascript] 
self.plugin.getselectedflash = function () { 
 
            return _getimagefromrange(self.edit.cmd.range, function (img) { 
 
                return img[0].classname == 'ke-flash'; 
 
            }); 
 
        }; 

在其下边添加如下代码:
[javascript] 
self.plugin.getselectedflv = function () { 
 
            return _getimagefromrange(self.edit.cmd.range,function (img) { 
 
                return img[0].classname =='ke-flv'; 
 
            }); 
 
        }; 

12.   修改kindeditor.js文件,找到代码_each('link,image,flash,media,anchor'.split(','), function (i, name),将其修改为_each('link,image,flash,flv,media,anchor'.split(','), function (i, name)
13.   修改kindeditor.js文件,找到代码return html.replace(/<img[^>]*class="?ke-(flash|rm|media)"?[^>]*>/ig, function (full),将其修改为return html.replace(/<img[^>]*class="?ke-(flash|flv|rm|media)"?[^>]*>/ig, function (full)