在 TinyMCE 4 图片上传中使用数据 URI 而不是 blob

Using data URI instead of blob in TinyMCE 4 image upload

使用 TinyMCE 4,我正在尝试做一个基本的本地文件选择器,例如 their example.

中使用的那个

在 运行 他们的例子之后,我注意到生成的图像源是一个 blob 而不是 base64。

所以我的问题是:是否可以使用 base64 而不是 blob?

我想到了 file_picker_callback callback would be used as the source of the image, so I tweaked the code using 的第一个参数,我将数据 URI 作为第一个参数传递。

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];
        var reader = new FileReader();

        reader.onload = function () {

            // 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 base64 = reader.result.split(',')[1];
            //var blobInfo = blobCache.create(id, file, base64);

            //blobCache.add( blobInfo );

            // call the callback and populate the Title field with the file name

            cb(reader.result, { title: 'hola' });
        };
        reader.readAsDataURL( file );
    };

    input.click();
}

但是它不起作用,而是将源转换为 blob,例如

<img src="blob:null/c8e90adb-4074-45b8-89f4-3f28c66591bb" alt="" /> 

如果我传递一个普通字符串,例如test.jpg,会生成

<img src="test.jpg" alt="" />

你看到的blob:格式其实就是Base64编码的二进制图片。如果你要 post TinyMCE 的内容到服务器你确实会得到 Base64 数据。

您可以强制 TinyMCE 立即将该图像发送到您的服务器以按照以下步骤转换为“常规”图像:

https://www.tinymce.com/docs/advanced/handle-async-image-uploads/

在tinymce\plugins\quickbars\plugin.js中添加如下代码,如图所示位置

$.ajax({
            url: 'saveupload', // Upload Script
            enctype : 'multipart/form-data',
            type: 'post',
            data: {"imageString":base64,"imageType":blob.type,"imageName": blob.name},
            success: function(responseText) {
                var myJSON = JSON.parse(responseText);
                editor.insertContent(editor.dom.createHTML('img', { src: myJSON }));
            },
            error : function(xhr, ajaxOptions, thrownError) {
            }
          });

注意:如果您使用缩小版,请使用任何缩小版工具(例如:yuicompressor)将其转换为缩小版 我正在上传图片到apache

servlet 代码如下

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {
    tbaService = new TBAServiceImpl();
    File f = new File("path");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Map<String, String[]> parameterNames = request.getParameterMap();
    Gson gson = new Gson();
    HttpSession session = request.getSession(true);

    long timeinMill = new Date().getTime();
    String uniqueFileName = "local_"+timeinMill+"_"+parameterNames.get("imageName")[0].replace(" ", "_");
    String fileType = parameterNames.get("imageType")[0].split("/")[1];
    try {

        BufferedImage image = null;
        byte[] imageByte;

        BASE64Decoder decoder = new BASE64Decoder();
        imageByte = decoder.decodeBuffer(parameterNames.get("imageString")[0]);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();

        // write the image to a file
        File outputfile = new File(filePath+uniqueFileName); //filePath = C:/Apache/htdocs/tba/images/
        ImageIO.write(image, fileType, outputfile);

        out.print(gson.toJson(uploadUrl+uniqueFileName)); //uploadUrl=http://localhost/test/images/
        out.flush();
        out.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}