简体   繁体   English

如何在AngularJS中使用CKEditor上传图像?

[英]How to upload an image with CKEditor in AngularJS?

Currently I am using angular-ckeditor to add CKEditor 4 . 目前,我正在使用angular-ckeditor添加CKEditor 4

In my template, I display it as: 在我的模板中,将其显示为:

<div ng-repeat="editor in editors">
  <div ckeditor="options" ng-model="editor.content"></div>
</div>

I'm looking for a way to upload images to CKEditor from desktop . 我正在寻找一种将图像从桌面上传到CKEditor的方法。 As far as I understand, angular-ckeditor and CKEditor libraries are separate, so I can add widgets and plugins easily. 据我了解, angular-ckeditor库和CKEditor库是分开的,因此我可以轻松添加小部件和插件。

The problem is that I can't seem to find the right plugins/widgets ( that do NOT use jQuery ), which will help me to upload an image from desktop. 问题是我似乎找不到合适的插件/小部件( 不使用jQuery ),这将帮助我从桌面上载图像。 I made it work only with image links. 我使它仅适用于图像链接。

There is not a lot of documentation about it on the official website. 官方网站上没有很多相关文档。 They say to use PHP files that will upload and browse images, but don't really explain how, especially with angular-ckeditor . 他们说要使用将上传和浏览图像的PHP文件,但并没有真正解释如何使用,特别是使用angular-ckeditor So I have several questions now: 所以我现在有几个问题:

  • Which plugins do I need for a simple image uploader, so that I can paste images into CKEditor? 一个简单的图像上传器需要哪些插件,以便可以将图像粘贴到CKEditor中?
  • How do I set it up with AngularJS? 如何使用AngularJS进行设置?
  • What does a PHP file uploader(/browser) look like? PHP文件上传器(/浏览器)是什么样的?

What I have tried so far doesn't even change the CKEditor tabs (it should change the image properties dialog by adding an "Upload" tab and some other UI). 到目前为止,我什至没有尝试更改CKEditor选项卡(它应该通过添加“上传”选项卡和某些其他UI 更改图像属性对话框)。 So clearly I'm missing a solid tutorial somewhere for all of this. 所以很明显,我在所有这些地方都缺少可靠的教程。

在此处输入图片说明

( I could also try to switch to ng-ckeditor , if a solution with this would be simpler ) 如果解决方案更简单, 我也可以尝试切换到 ng-ckeditor

First, let's take a look at some basics without Angular. 首先,让我们看一些没有Angular的基础知识。 For CKEditor version 4, we can initialize an editor with the filebrowserImageUploadUrl configuration option, which enables functionality from the File Browser API : 对于CKEditor版本4,我们可以使用filebrowserImageUploadUrl配置选项初始化一个编辑器,该选项启用File Browser API的功能

CKEDITOR.replace('editor', { 
    filebrowserImageUploadUrl: '/upload.php?type=image' 
});

This just loads an editor instance onto a <textarea name="editor"> . 这只是将编辑器实例加载到<textarea name="editor"> Because we set the filebrowserImageUploadUrl option, the Upload tab becomes available in the editor's image dialog. 由于我们设置了filebrowserImageUploadUrl选项,因此在编辑器的图像对话框中可以使用“上载”选项卡。 The example value, /upload.php?type=image , is the URL to the PHP script on the server that handles the uploaded image files. 示例值/upload.php?type=image是服务器上处理上传的图像文件的PHP脚本的URL。

When a user uploads an image, CKEditor will send the image to this URL on the server. 当用户上传图像时,CKEditor会将图像发送到服务器上的该URL。 The handler at this URL should validate the request, resize the image (if needed), and move the uploaded image to a permanent location on the server. 此URL上的处理程序应验证请求,调整图像大小(如果需要),并将上载的图像移动到服务器上的永久位置。 Then, the handler sends an HTML response back to CKEditor with the image's public URL. 然后,处理程序将HTML响应连同图像的公共URL发送回CKEditor。

Of course, we can write the server-side handler in any language. 当然,我们可以用任何语言编写服务器端处理程序。 Here's a basic example for PHP that we'll save as upload.php : 这是PHP的基本示例,我们将另存为upload.php

<?php 
$tempName = $_FILES['upload']['tmp_name'];
$fileName = uniqid() . $_FILES['upload']['name'];
$uploadPath = '/path/to/uploads/' . $fileName;
$imageUrl = 'http://example.com/uploads/' . $fileName;

$success = move_uploaded_file($tempName, $uploadPath);

$html = '<script>window.parent.CKEDITOR.tools.callFunction(%s, "%s", "%s");</script>';
$message = $success ? 'Uploaded successfully.' : 'Upload failed.';
echo sprintf($html, $_GET['CKEditorFuncNum'], $imageUrl, $message);

This script places an uploaded image into the web server's uploads/ directory so the browser can fetch the image. 该脚本将上传的图像放置到Web服务器的uploads /目录中,以便浏览器可以获取图像。 It passes back the CKEditorFuncNum parameter from the request to identify the appropriate callback for the upload. 它从请求中传回CKEditorFuncNum参数,以标识上载的适当回调。 This example provides some basic protection against duplicate filenames, but, for a real-world application, we'd need to add security, validation, and error handling (authentication, CSRF, file type checking, max size, file name sanitization, etc.). 此示例提供了一些基本的保护措施,以防止重复的文件名,但是,对于实际应用程序,我们需要添加安全性,验证和错误处理(身份验证,CSRF,文件类型检查,最大大小,文件名清理等)。 )。


So far, this all works with CKEditor's standard, built-in functionality (no plugins, Angular, or jQuery needed). 到目前为止,所有这些都可以与CKEditor的标准内置功能一起使用(不需要插件,Angular或jQuery)。 To enable users to drag-and-drop or paste images into the editor, we can add the Upload Image plugin to our CKEditor build (or use the standard-all distribution from the CKEditor CDN ). 为了使用户能够将图像拖放或粘贴到编辑器中,我们可以将Upload Image插件添加到CKEditor构建中(或使用CKEditor CDN中standard-all发行版)。

We need to declare the plugin when initializing the editor: 我们需要在初始化编辑器时声明插件:

CKEDITOR.replace('editor', { 
    extraPlugins: 'uploadimage',
    filebrowserImageUploadUrl: '/upload.php?type=image' 
});

...and then extend our upload.php script to return the JSON response expected by the plugin. ...然后扩展我们的upload.php脚本以返回插件期望的JSON响应。 Add this block before the last three lines of the previous example: 在上一个示例的最后三行之前添加此块:

if (isset($_GET['responseType']) && $_GET['responseType'] === 'json') {
    echo json_encode([
        'uploaded' => $success,
        'fileName' => $fileName,
        'url' => $imageUrl,
    ]);

    return;
}

The Upload Image plugin sends the responseType=json URL parameter that the server-side script can check for to determine which type of response to send back. Upload Image插件会发送responseType=json URL参数,服务器端脚本可以检查该参数以确定发送回哪种响应。


Finally, let's take a look at how to initialize an editor using the angular-ckeditor package described in the question: 最后,让我们看一下如何使用问题中描述的angular-ckeditor包初始化编辑器:

<div ng-controller="EditorCtrl as editor">
    <textarea ckeditor="editor.options" ng-model="editor.content"></textarea>
</div>
var myApp = angular.module('myApp', ['ckeditor'])

myApp.controller('EditorCtrl', function () {
    this.options = {
        extraPlugins: 'uploadimage',
        filebrowserImageUploadUrl: '/image-upload.php?type=image'
    };
});

As we can see, we don't need to do much to "angularize" this. 正如我们所看到的,我们不需要做很多事情就可以将它“角度化”。 Once we create our template, we declare the same configuration options that we'd pass to the plain CKEDITOR.replace() on a controller that we reference on an element with the ckeditor directive. 创建模板后,我们声明传递给使用ckeditor指令在元素上引用的控制器上的普通CKEDITOR.replace() 相同的配置选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM