Froala - 图片上传JSON响应

时间:2014-02-14 09:37:47

标签: javascript php json

我在网站上使用Froala editor。这个编辑器内置图像上传功能 - 图像上传工作正常,但我遇到了响应问题。

这来自文档:

The server processes the HTTP request.
The server has to process the upload request, save the image and return a hashmap containing a link to the uploaded image. The returned hashmap needs to look like: { link: 'path_to_uploaded_file/file.png' }

这是我返回链接的功能:

public function froala_upload()
    {
        header('Content-type: application/json');
        $folder = 'public/img/media';
        $slika = $this->site->single_upload($folder, 'jpg|jpeg|png|bmp|JPG|JPEG|PNG|BMP');
        $link = array("link" => $slika);

        echo json_encode($link);
    }

这是JS代码:

$('textarea').editable({
  inlineMode: false,
  imageUploadParam: "userfile",
  imageUploadURL: "<?php echo base_url() ?>admin/froala_upload",
  // Set the image error callback.
  imageErrorCallback: function (data) {
      // Bad link.
      if (data.errorCode == 1) {
        console.log(data);
      }

      // No link in upload response.
      else if (data.errorCode == 2) {
        console.log(data);
      }

      // Error during file upload.
      else if (data.errorCode == 3) {
        console.log(data);
      }
  }
});

当我上传图片时,我收到以下错误:

Object { errorCode=1, errorStatus="Bad link."}

这是我得到的回应:

{"link":"7d59d61.jpg"}

什么似乎有问题?

Froala image upload documentation

2 个答案:

答案 0 :(得分:1)

您必须返回绝对图像路径:

$link = array("link" => '/img/media/'.$slika);

因为Froala将其查找到http://example.com/7d59d61.jpg

答案 1 :(得分:1)

问题是无法从返回的链接加载图像。您必须确保可以从中访问图像。 Froala编辑器使用您从src标记中为img属性返回的链接。我认为你必须做类似的事情:

$link = array("link" => $slika . 'public/img/media');