【问题标题】:Tinymce get image _description with file_picker_callback and image uploaderTinymce 使用 file_picker_callback 和图片上传器获取图片描述
【发布时间】:2017-03-26 19:37:10
【问题描述】:

TL:DR我正在尝试使用 javascript 获取 image_description 字段的值,以将其传递给我的 post xhr 请求

下面的原始问题

我正在使用 file_picker_callback 类型的图像 https://www.tinymce.com/docs/configure/file-image-upload/#file_picker_callback

我在我的

中启用了 image_description 输入字段
tinymce.init({
     ....,
     image_description: true,
     ...

一切都很好,但我想传递 image_description 字段以及存储在数据库中。但我无法抓取数据

这是我的两个函数,直接取自 tinymce 网站

  file_picker_callback: function(cb, value, meta) {

    var input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');

    input.onchange = function() {
      console.log(this.files);
      var file = this.files[0];

      console.log( meta ); //I thought it might be here in the meta bt it isn't

     console.log( $('#mceu_62').val() ); //I tried to get it from its id but it returns undefined i think that field is created after this function is created.



      var id = file.name;
      var blobCache = tinymce.activeEditor.editorUpload.blobCache;
      var blobInfo = blobCache.create(id, file);
      blobCache.add(blobInfo);


      // call the callback and populate the Title field with the file name
      cb(blobInfo.blobUri(), { title: file.name });
    };

    input.click();
  },
  images_upload_handler: function (blobInfo, success, failure) {
      var xhr, formData;

      xhr = new XMLHttpRequest();
      xhr.withCredentials = false;
      xhr.open('POST', '/articles/postAcceptor');

      xhr.onload = function() {
        var json;

        if (xhr.status != 200) {
          failure('HTTP Error: ' + xhr.status);
          return;
        }

        json = JSON.parse(xhr.responseText);

        if (!json || typeof json.location != 'string') {
          failure('Invalid JSON: ' + xhr.responseText);
          return;
        }

        success(json.location);
      };

      formData = new FormData();
      formData.append('file', blobInfo.blob(), blobInfo.filename());
      formData.append('description', /*but i can't get the value*/);

      xhr.send(formData);
    }

@卡尔·莫里森

【问题讨论】:

    标签: javascript tinymce-4


    【解决方案1】:

    试试这个来获得价值:

    images_upload_handler: function (blobInfo, success, failure) {
          var xhr, formData;
    
          xhr = new XMLHttpRequest();
          xhr.withCredentials = false;
          xhr.open('POST', '/articles/postAcceptor');
    
          xhr.onload = function() {
            var json;
    
            if (xhr.status != 200) {
              failure('HTTP Error: ' + xhr.status);
              return;
            }
    
            json = JSON.parse(xhr.responseText);
    
            if (!json || typeof json.location != 'string') {
              failure('Invalid JSON: ' + xhr.responseText);
              return;
            }
    
            success(json.location);
          };
    
    
          var description = '';
    
          jQuery(tinymce.activeEditor.dom.getRoot()).find('img').not('.loaded-before').each(
        function() {
            description = $(this).attr("alt");
            $(this).addClass('loaded-before');
        });
    
          formData = new FormData();
          formData.append('file', blobInfo.blob(), blobInfo.filename());
          formData.append('description', description); //found now))
    
          xhr.send(formData);
        }
    

    【讨论】:

    • 从body/textarea返回数据我正在寻找图片描述的弹出字段的数据
    • 天哪,这是什么魔法,你懂的。我没有意识到描述窗口字段是 alt 标签。这个“加载前”在哪里,不知道在哪里。我以为你在拖钓我,赏金好赚。非常感谢。
    • 当临时 url 被添加时,我通过“GET data:image/jpeg;base64,undefined net::ERR_INVALID_URL”得到一个错误你知道该怎么做吗?
    • @artSir 答案再次更新)。尝试新的代码版本
    • @artSir ''loaded-before' - 我的自定义类。在我们处理完图片之后,我们应该添加这个类。如果图片中没有类,那么这是最后下载的图片。此功能适用于当我们想要在文章中加载多于一张图片的情况
    【解决方案2】:

    以下内容可能会对您有所帮助 https://www.tinymce.com

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script>
    </head>
    <body>
    <textarea>Next, get a free TinyMCE Cloud API key!</textarea>
    </body>
    </html>
    

    【讨论】:

    • 拖钓?这只是初始化 tinyMCE
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多