如何将自定义文件浏览器/上传器与 CKEditor 集成?
官方文档不太清楚 - 将自定义文件浏览器/上传器与 CKEditor 集成的正确方法是什么? (v3 - 不是 FCKEditor)
The official documentation is less than clear - what's the correct way to integrate a custom file browser/uploader with CKEditor? (v3 - not FCKEditor)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先在实例化 CKEditor 时注册您的自定义浏览器/上传器。您可以为图像浏览器和一般文件浏览器指定不同的 URL。
您的自定义代码将收到一个名为 CKEditorFuncNum 的 GET 参数。保存它 - 这就是您的回调函数。假设您将其放入
$callback
中。当某人选择一个文件时,运行此 JavaScript 来通知 CKEditor 选择了哪个文件:
其中“url”是他们选择的文件的 URL。可选的第三个参数可以是您想要在标准警报对话框中显示的文本,例如“非法文件”或其他内容。如果第三个参数是错误消息,则将 url 设置为空字符串。
CKEditor 的“上传”选项卡将在“上传”字段中提交一个文件 - 在 PHP 中,该文件将转到 $_FILES['upload']。 CKEditor 希望您的服务器输出的是完整的 JavaScript 块:
同样,您需要为其提供回调参数、文件的 URL 以及可选的消息。如果消息是空字符串,则不会显示任何内容;如果消息有错误,则 url 应为空字符串。
官方 CKEditor 文档在这方面并不完整,但如果您遵循上述内容,它会像冠军一样工作。
Start by registering your custom browser/uploader when you instantiate CKEditor. You can designate different URLs for an image browser vs. a general file browser.
Your custom code will receive a GET parameter called CKEditorFuncNum. Save it - that's your callback function. Let's say you put it into
$callback
.When someone selects a file, run this JavaScript to inform CKEditor which file was selected:
Where "url" is the URL of the file they picked. An optional third parameter can be text that you want displayed in a standard alert dialog, such as "illegal file" or something. Set url to an empty string if the third parameter is an error message.
CKEditor's "upload" tab will submit a file in the field "upload" - in PHP, that goes to $_FILES['upload']. What CKEditor wants your server to output is a complete JavaScript block:
Again, you need to give it that callback parameter, the URL of the file, and optionally a message. If the message is an empty string, nothing will display; if the message is an error, then url should be an empty string.
The official CKEditor documentation is incomplete on all this, but if you follow the above it'll work like a champ.
我发布了一篇关于将旧 FCKEditor 中可用的 FileBrowser 集成到 CKEditor 中的小教程。
http://www.mixedwaves.com/2010/02/ integrating-fckeditor-filemanager-in-ckeditor/
它包含执行此操作的分步说明,而且非常简单。我希望任何正在寻找此内容的人都会发现本教程很有帮助。
I have posted one small tutorial about integrating the FileBrowser available in old FCKEditor into CKEditor.
http://www.mixedwaves.com/2010/02/integrating-fckeditor-filemanager-in-ckeditor/
It contains step by step instructions for doing so and its pretty simple. I hope anybody in search of this will find this tutorial helpful.
我自己刚刚经历了学习过程。我明白了,但我同意文档的编写方式对我来说有点令人生畏。对我来说最重要的时刻是理解对于浏览,CKeditor 所做的就是打开一个新窗口并在 url 中提供一些参数。它允许您添加其他参数,但请注意您需要对您的值使用encodeURIComponent()。
我将浏览器和上传器称为
浏览器,在打开的窗口(browse.php)中,您使用 php &使用 Node.js 提供选项列表,然后在提供的 onclick 处理程序上,调用带有两个参数的 CKeditor 函数,即所选图像的 url/路径和 CKeditor 在 url 中提供的 CKEditorFuncNum:
Simarly,上传者 只需调用您提供的 url,例如 upload.php,并再次提供 $_GET['CKEditorFuncNum']。目标是
一个 iframe,因此,在从 $_FILES 保存文件后,您可以将反馈传递给 CKeditor,如下所示:
下面是一个简单易懂的自定义浏览器脚本。虽然它不允许用户在服务器中导航,但它允许您在调用浏览器时指示从哪个目录提取图像文件。
这都是相当基本的编码,因此它应该适用于所有相对现代的浏览器。
CKeditor 仅打开一个新窗口,其中提供了 URL
// ========= 下面的 browse.php 的完整代码
I just went through the learning process myself. I figured it out, but I agree the documentation is written in a way that was sorta intimidating to me. The big "aha" moment for me was understanding that for browsing, all CKeditor does is open a new window and provide a few parameters in the url. It allows you to add additional parameters but be advised you will need to use encodeURIComponent() on your values.
I call the browser and the uploader with
For the browser, in the open window (browse.php) you use php & js to supply a list of choices and then upon your supplied onclick handler, you call a CKeditor function with two arguments, the url/path to the selected image and CKEditorFuncNum supplied by CKeditor in the url:
Simarly, the uploader simply calls the url you supply, e.g., upload.php, and again supplies $_GET['CKEditorFuncNum']. The target is
an iframe so, after you save the file from $_FILES you pass your feedback to CKeditor as thus:
Below is a simple to understand custom browser script. While it does not allow users to navigate around in the server, it does allow you to indicate which directory to pull image files from when calling the browser.
It's all rather basic coding so it should work in all relatively modern browsers.
CKeditor merely opens a new window with the url provided
// ========= complete code below for browse.php
我花了一段时间试图解决这个问题,这就是我所做的。我已经非常简单地将其分解,因为这就是我所需要的。
在 ckeditor 文本区域正下方,输入上传文件,如下所示 >>>
'然后添加你的上传文件,这是我的,用 ASP 编写的。如果您使用 PHP 等,只需将 ASP 替换为您的上传脚本,但确保页面输出相同的内容。
I spent a while trying to figure this one out and here is what I did. I've broken it down very simply as that is what I needed.
Directly below your ckeditor text area, enter the upload file like this >>>>
'and then add your upload file, here is mine which is written in ASP. If you're using PHP, etc. simply replace the ASP with your upload script but make sure the page outputs the same thing.
这是我用过的方法。它非常简单,而且运行得很好。
在 CK 编辑器根目录中有一个名为 config.js 的文件,
我添加了这个文件(您不需要查询字符串,这仅适用于我们的文件管理器)。我还包括一些外观和更改显示的默认按钮:
然后,我们的文件管理器调用此:
This is the approach I've used. It's quite straightforward, and works just fine.
In the CK editor root directory there is a file named config.js
I added this (you don't need the querystring stuff, this is just for our file manager). I also included some skinning and changing of the default buttons shown:
Then, our file manager calls this:
Zerokspot 上的一篇文章,标题为CKEditor 3.0 中的自定义文件浏览器回调 处理这个。最相关的部分引用如下:
An article at zerokspot entitled Custom filebrowser callbacks in CKEditor 3.0 handles this. The most relevant section is quoted below:
首先在实例化 CKEditor 时注册您的自定义浏览器/上传器。
上传文件的代码(ckFileUpload.php) &将上传文件放在项目的根目录下。
在为自定义文件上传进行了大量研发之后,CK-editor 文档不清楚,最终我找到了这个解决方案。它对我有用,我希望它对其他人也有帮助。
Start by registering your custom browser/uploader when you instantiate CKEditor.
Code for upload file(ckFileUpload.php) & put the upload file on root dir of your project.
Ck-editor documentation is not clear after doing alot of R&D for custom file upload finally i have found this solution. It work for me and i hope it will helpful to others as well.
对于想了解 Servlet/JSP 实现的人,您可以按照以下步骤进行操作...我还将在下面解释 uploadimage。
1) 首先确保您已将 filebrowser 和 uploadimage 变量添加到您的
config.js 文件。让您的插件文件夹中也有 uploadimage 和 filebrowser 文件夹。
2)这部分是让我绊倒的地方:
Ckeditor网站文档说你需要使用这两种方法:
因此,如果您在页面 editor.jsp 中初始化了 ckeditor,那么您需要在页面 filebrowser.jsp 中创建一个文件浏览器(带有基本的 html/css/javascript)。
editor.jsp(您所需要的只是脚本标记中的这个)当您单击“浏览服务器”按钮时,此页面将在迷你窗口中打开 filebrowser.jsp。
filebrowser.jsp(是您构建的自定义文件浏览器,其中将包含上述方法)
3) FileBrowser Servlet
4) UploadImage Servlet
Go返回 ckeditor 的 config.js 文件并添加以下行:
然后您也可以拖放文件:
这就是全部了。希望它能帮助某人。
For people wondering about a Servlet/JSP implementation here's how you go about doing it... I will be explaining uploadimage below also.
1) First make sure you have added the filebrowser and uploadimage variable to your
config.js file. Make you also have the uploadimage and filebrowser folder inside the plugins folder too.
2) This part is where it tripped me up:
The Ckeditor website documentation says you need to use these two methods:
So if you have ckeditor initialized in page editor.jsp then you need to create a file browser (with basic html/css/javascript) in page filebrowser.jsp.
editor.jsp (all you need is this in your script tag) This page will open filebrowser.jsp in a mini window when you click on the browse server button.
filebrowser.jsp (is the custom file browser you built which will contain the methods mentioned above)
3) The FileBrowser Servlet
4) UploadImage Servlet
Go back to your config.js file for ckeditor and add the following line:
Then you can drag and drop files also:
And that's all folks. Hope it helps someone.