【问题标题】:Can't upload image to local server with Froala editor无法使用 Froala 编辑器将图像上传到本地服务器
【发布时间】:2016-11-29 19:13:21
【问题描述】:

我正在尝试使用 Froala 所见即所得编辑器将图像上传到我的本地主机(用于测试目的),但它不起作用。当我选择要上传的图像时,它在编辑器中显示为褪色,然后在我单击其他位置时消失。下面是我的代码,以及我尝试关注的他们文档的链接。

文档: https://www.froala.com/wysiwyg-editor/docs/concepts/image/upload

也有这个方法,但是不知道把php放在哪里:https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload

我的密码

(“fedit”是我用于文本区域的类。)

Javascript:

<script>
  $(function() {
    $('.fedit').froalaEditor({
        // Set the image upload parameter.
        imageUploadParam: 'file',

        // Set the image upload URL.
        imageUploadURL: '/upload_image.php',

        // Additional upload params.
        imageUploadParams: {class: 'fedit'},

        // Set request type.
        imageUploadMethod: 'POST',

        // Set max image size to 5MB.
        imageMaxSize: 5 * 1024 * 1024,

        // Allow to upload PNG and JPG.
        imageAllowedTypes: ['jpeg', 'jpg', 'png']
      })
      .on('froalaEditor.image.beforeUpload', function (e, editor, images) {
        // Return false if you want to stop the image upload.
      })
      .on('froalaEditor.image.uploaded', function (e, editor, response) {
        // Image was uploaded to the server.
      })
      .on('froalaEditor.image.inserted', function (e, editor, $img, response) {
        // Image was inserted in the editor.
      })
      .on('froalaEditor.image.replaced', function (e, editor, $img, response) {
        // Image was replaced in the editor.
      })
      .on('froalaEditor.image.error', function (e, editor, error, response) {
        // Bad link.
        else if (error.code == 1) { ... }

        // No link in upload response.
        else if (error.code == 2) { ... }

        // Error during image upload.
        else if (error.code == 3) { ... }

        // Parsing response failed.
        else if (error.code == 4) { ... }

        // Image too text-large.
        else if (error.code == 5) { ... }

        // Invalid image type.
        else if (error.code == 6) { ... }

        // Image can be uploaded only to same domain in IE 8 and IE 9.
        else if (error.code == 7) { ... }

        // Response contains the original server response to the request if available.
      });
  });
</script>

上传图片.php

<?php
    // Allowed extentions.
    $allowedExts = array("gif", "jpeg", "jpg", "png", "blob");

    // Get filename.
    $temp = explode(".", $_FILES["file"]["name"]);

    // Get extension.
    $extension = end($temp);

    // An image check is being done in the editor but it is best to
    // check that again on the server side.
    // Do not use $_FILES["file"]["type"] as it can be easily forged.
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

    if ((($mime == "image/gif")
    || ($mime == "image/jpeg")
    || ($mime == "image/pjpeg")
    || ($mime == "image/x-png")
    || ($mime == "image/png"))
    && in_array(strtolower($extension), $allowedExts)) {
        // Generate new random name.
        $name = sha1(microtime()) . "." . $extension;

        // Save file in the uploads folder.
        move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/ " . $name);

        // Generate response.
        $response = new StdClass;
        $response->link = "/uploads/" . $name;
        echo stripslashes(json_encode($response));
    }
?>

【问题讨论】:

  • 这里没有足够的信息,你的 JS 控制台给你任何信息或者你的服务器上的错误日志怎么样。
  • 感谢您的回复。我的日志中出现错误:加载资源失败:服务器响应状态为 404 (Not Found)localhost/froala_uploads 。然后当我尝试添加图像时出现另一个错误: image.min.js:7 GET localhost/froala_uploads 404 (Not Found)f @ image.min.js:7dispatch @ jquery.js:3r.handle @ jquery.js:3 .顺便说一句,我将文件夹更改为“froala_uploads”,只是为了更容易识别。
  • 他们在他们的网站上提供了一个 SDK,你为什么不使用它?
  • 不幸的是,我在编程方面真的没有那么先进。我非常了解 php,但从未完全理解 SDK 是什么。这是您使用的东西而不是“upload_image.php”,还是与它一起使用?froala.com/wysiwyg-editor/docs/sdks/php.
  • froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload 我不确定是否应该将 php 代码放在一个名为 upload_image.php 的单独文件中,还是该文件仍应包含上述代码?

标签: javascript php ajax froala


【解决方案1】:

这是使用 SDK for php 使其在本地主机上工作的解决方案。 (我正在使用 WAMP)。感谢上面 cmets 中的 cmorrissey 让我研究了 sdk。

在此处下载 SDK 文件: https://www.froala.com/wysiwyg-editor/docs/sdks/php

按照以下说明进行操作: https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload

但我会向您展示代码并提及对我来说是个问题的几点。

将此 javascript 放在您的 textarea 所在页面的底部:

<script>
  $(function() {
    $('.selector').froalaEditor({
      // Set the image upload URL.
      imageUploadURL: '/mywebsite/upload_image.php'
    })
  });
</script>

请注意,您的“imageUploadURL:”(upload_image.php) 路径指向您本地服务器的根目录。例如。 “http://localhost”指向的任何地方。因此,在我的情况下,我的网站位于根目录的子文件夹中,因此我将放置“/mywebsite/upload_image.php”(实际上位于 C:\wamp64\www\mywebsite 中)。

在upload_image.php文件中:

<?php

// Include the editor SDK.
require 'froala/sdk/lib/FroalaEditor.php';

// Store the image.
try {
  $response = FroalaEditor_Image::upload('/mywebsite/img/uploads/');
  echo stripslashes(json_encode($response));
}
catch (Exception $e) {
  http_response_code(404);
}

?>

如您所见,我将 sdk“lib”文件夹(您在上面下载的)复制到了我创建的名为“froala/sdk/”的子文件夹中。你可以把它放在任何你想要的地方。

另一个重要的快速说明是他们所做的一个错字。在他们的示例 php 代码中,他们告诉您该文件名为“froala_editor.php”,而在下载的文件中它实际上名为“FroalaEditor.php”。这是我遇到的问题之一。

现在一切都应该正常了。当您将其上传到您的网络服务器时,您只需修改路径即可。

【讨论】:

  • 我遇到了同样的问题,在我的本地测试。我访问一个本地文件并尝试在编辑器上显示,同样的问题发生了。但我正在使用 typescript 和 angular 2。你能帮忙吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2019-09-30
  • 2015-04-27
  • 2011-09-12
相关资源
最近更新 更多