Tinymce 4 file_browser_callback:打开本地文件浏览器的功能

时间:2016-02-13 19:00:34

标签: tinymce image-uploading

我在我的网络应用程序上使用Tinymce 4.0。但我真的不知道如何做到这一点:当用户点击浏览按钮时,如何打开本地文件浏览器并从该本地文件浏览器向对话框窗口添加图像URL?我有这个源代码:

file_browser_callback: function(field_name, url, type, win) {win.document.getElementById(field_name).value = ''; }

但我不知道如何解决这个问题。我不需要特殊的文件浏览器,但可以打开本地文件浏览器

1 个答案:

答案 0 :(得分:0)

此处来自TinyMCE docs的Basic Local File Picker演示文稿的片段:

注意:不幸的是,它似乎不能作为StackOverflow片段在这里工作。



tinymce.init({
  selector: '#editor',
  plugins: 'image code',
  toolbar: 'undo redo | link image | code',
  // enable title field in the Image dialog
  image_title: true, 
  // enable automatic uploads of images represented by blob or data URIs
  automatic_uploads: true,
  // URL of our upload handler (for more details check: https://www.tinymce.com/docs/configure/file-image-upload/#images_upload_url)
  images_upload_url: 'postAcceptor.php',
  // here we add custom filepicker only to Image dialog
  file_picker_types: 'image', 
  // and here's our custom image picker
  file_picker_callback: function(cb, value, meta) {
    var input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');
    
    // Note: In modern browsers input[type="file"] is functional without 
    // even adding it to the DOM, but that might not be the case in some older
    // or quirky browsers like IE, so you might want to add it to the DOM
    // just in case, and visually hide it. And do not forget do remove it
    // once you do not need it anymore.

    input.onchange = function() {
      var file = this.files[0];
      
      // Note: Now we need to register the blob in TinyMCEs image blob
      // registry. In the next release this part hopefully won't be
      // necessary, as we are looking to handle it internally.
      var id = 'blobid' + (new Date()).getTime();
      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();
  }
});

<script src="//cdn.tinymce.com/4/tinymce.js"></script>
<textarea id="editor"></textarea>
&#13;
&#13;
&#13;