tinymce使用文件浏览器添加图像

时间:2019-03-21 12:17:39

标签: javascript php html tinymce

我在Web博客上具有以下用于添加帖子功能的代码,我具有允许选择文件的按钮,但它不起作用。有什么帮助吗?

      tinymce.init({

          selector: "textarea",
          plugins: [
              "advlist autolink lists link image charmap print preview anchor",
              "searchreplace visualblocks code fullscreen",
              "insertdatetime media table contextmenu paste"
          ],
          toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",

          file_browser_callback : 'myFileBrowser'


      });

2 个答案:

答案 0 :(得分:1)

我希望它将对您有帮助

 tinymce.init({
      selector: 'textarea',  // change this value according to your HTML
      images_upload_handler: function (blobInfo, success, failure) {
        var xhr, formData;

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

        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());

        xhr.send(formData);
      }
    });

更多详情

https://www.tiny.cloud/docs/configure/file-image-upload/

答案 1 :(得分:1)

var fileUploadUrl = "{{route('fileUploadEditor')}}";

tinymce.init({
    selector: 'textarea',  // change this value according to your HTML
    images_upload_handler: function (blobInfo, success, failure) {
    var xhr, formData;

    xhr = new XMLHttpRequest();
    xhr.withCredentials = false;
    xhr.open('POST',fileUploadUrl);

    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());

    xhr.send(formData);
  }
});