TinyMCE 6图片上传异常求助:无src且上传请求未触发
关于TinyMCE 6图片上传的问题求助
被TinyMCE 6的图片上传问题困扰一周,查过Stack Overflow及官方文档。
此前问题:图片以blobxxx.jpg名称上传(PHP处理程序也返回该名称),但编辑器保存的内容中图片无src属性,且显示的正确文件名与上传的blobxxx文件名不匹配。
当前问题:图片完全无法上传,images_upload_url对应的PHP接口似乎未被调用,未修改相关配置。尝试移除image_title配置,仅导致图片无标题,问题依旧;设置automatic_uploads:true时,日志出现指向images_upload_url对应PHP文件路径的错误GET请求。
参考了TinyMCE 6官方图片上传文档及PHP上传处理文档。
TinyMCE初始化代码
tinymce.init({ selector: "#mytextarea", plugins: ["link","image"], toolbar: "undo redo | image | blocks | bold italic | alignleft aligncenter alignright alignjustify | outdent indent", tinycomments_mode: "embedded", /* enable title field in the Image dialog*/ image_title: true, /* enable automatic uploads of images represented by blob or data URIs*/ automatic_uploads: false, images_upload_url: "<?php echo site_url("manage/project/tinyMCE_upload");?>", file_picker_types: 'image', file_picker_callback: (cb, value, meta) => { const input = document.createElement('input'); input.setAttribute('type', 'file'); input.setAttribute('accept', 'image/*'); input.addEventListener('change', (e) => { const file = e.target.files[0]; const reader = new FileReader(); reader.addEventListener('load', () => { /* 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. */ const id = 'blobid' + (new Date()).getTime(); const blobCache = tinymce.activeEditor.editorUpload.blobCache; const base64 = reader.result.split(',')[1]; const blobInfo = blobCache.create(id, file, base64); blobCache.add(blobInfo); /* call the callback and populate the Title field with the file name */ cb(blobInfo.blobUri(), { title: file.name }); }); reader.readAsDataURL(file); }); input.click(); }, relative_urls : false, remove_script_host : false });
PHP处理代码
public function tinyMCE_upload() { if (isset($_SERVER['HTTP_ORIGIN'])) { header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); } // Don't attempt to process the upload on an OPTIONS request if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { header("Access-Control-Allow-Methods: POST, OPTIONS"); return; } reset ($_FILES); $temp = current($_FILES); if (is_uploaded_file($temp['tmp_name'])){ /* If your script needs to receive cookies, set images_upload_credentials : true in the configuration and enable the following two headers. */ // header('Access-Control-Allow-Credentials: true'); // header('P3P: CP="There is no P3P policy."'); // Sanitize input if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) { header("HTTP/1.1 400 Invalid file name."); return; } // Verify extension if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) { header("HTTP/1.1 400 Invalid extension."); return; } // Accept upload if there was no origin, or if it is an accepted origin $filetowrite = $temp['name']; move_uploaded_file($temp['tmp_name'], $filetowrite); // Determine the base URL $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://"; $baseurl = $protocol . $_SERVER["HTTP_HOST"] . rtrim(dirname($_SERVER['REQUEST_URI']), "/") . "/"; // Respond to the successful upload with JSON. // Use a location key to specify the path to the saved image resource. // { location : '/your/uploaded/image/file'} echo json_encode(array('location' => $filetowrite)); } else { // Notify editor that the upload failed header("HTTP/1.1 500 Server Error"); } }
恳请各位提供帮助!
内容的提问来源于stack exchange,提问作者xanabobana
相关产品推荐
相关产品推荐

