Tinymce - images_upload_handler语言 - 验证最大文件大小



有没有办法在Tinymce 5的images_upload_handler中验证图像文件大小。

tinymce.init({
selector: '#mytextarea',
images_upload_handler: function(blobInfo, success, failure) {
.......
}
});

该函数有 3 个参数,第一个是所选图像的 blob 内容。没有用于检查 Blob 内容大小的规定。

有什么办法吗?

blobInfo.blob().size

给出上传图像的大小(以字节为单位(,失败会引发错误:上传图像失败:...

每次在编辑器中上传、粘贴或编辑图像时,都会触发该函数。

当上传或编辑的图像大于max_size时,此代码会引发错误

images_upload_handler: function (blobInfo, success, failure) {
var image_size = blobInfo.blob().size / 1000;  // image size in kbytes
var max_size   = max_size_value                // max size in kbytes
if( image_size  > max_size ){        
failure('Image is too large( '+ image_size  + ') ,Maximum image size is:' + max_size + ' kB');
return;      
}else{
// Your code
}

在 TinyMCE v6 中:

const example_image_upload_handler = (blobInfo, progress) => new Promise((resolve, reject) => {
// In case which the max file size is 1Mb
if (blobInfo.blob().size > 1024 * 1024) {
return reject({message: 'File is too big!', remove: true});
}
// Do the rest
});
tinymce.init({
selector: 'textarea',
images_upload_handler: example_image_upload_handler
});

有关如何发送 POST 请求,请参阅 https://www.tiny.cloud/docs/tinymce/6/upload-images/#example-using-images_upload_handler。

最新更新