【发布时间】:2018-08-18 06:03:13
【问题描述】:
使用 TinyMCE 4,我正在尝试做一个基本的本地文件选择器,例如 their example 中使用的那个。
运行他们的示例后,我注意到生成的图像源是一个 blob,而不是 base64。
所以我的问题是:是否可以使用 base64 代替 blob?
我认为file_picker_callback 回调的第一个参数将用作图像的源,因此我使用this answer 调整了代码,其中我将数据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="" />
【问题讨论】:
标签: javascript base64 blob image-uploading tinymce-4