如何使用文件选择器处理程序和images_upload_handler上传带有tinymce的图像

时间:2017-03-22 14:05:52

标签: javascript tinymce

默认情况下,tinymce图像没有浏览按钮,您可以在其中单击并查看用于选择图像的对话框。在我的代码中我试图将图像选择器按钮添加到tinymce但是发现很难将它与images_upload_handler结合起来。最后,我如何使用成功回调来更新images_upload_base_path。

tinymce.init({
    ...
    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);
    }
});

1 个答案:

答案 0 :(得分:1)

我真的不明白为什么,但对我来说images_upload_handler不能正常运作...... =(

使用file_picker_callback代替,就像我在这个例子中找到的那样: https://codepen.io/nirajmchauhan/pen/EjQLpV