自TinyMCE 4.x起,TinyMCE内置了图像上传功能,但我无法使用它。
我的代码:
tinymce.init({
selector: "textarea",
plugins: [
"image","code","autoresize","media","link","imagetools"
],
link_class_list: [
{title: 'None', value: ''},
{title: 'PDF', value: 'pdf'}
],
default_link_target: "_blank",
images_upload_url: "uploadImage.php",
toolbar1: "insertfile undo | redo | styleselect | bold | italic | alignleft | aligncenter | alignright | alignjustify | bullist | numlist | outdent | indent | link | image | media | code",
toolbar2: "",
menubar : false,
statusbar : false,
convert_urls: false,
content_css: 'editor-style.css',
width: 800
});
该行:" images_upload_url" shoud负责图片上传。附加到此的php文件如下:
$imageFolder = "../images/tinymce";
reset ($_FILES);
$temp = current($_FILES);
if (is_uploaded_file($temp['tmp_name'])){
if (isset($_SERVER['HTTP_ORIGIN'])) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}
if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
header("HTTP/1.0 500 Invalid file name.");
return;
}
if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
header("HTTP/1.0 500 Invalid extension.");
return;
}
$filetowrite = $imageFolder . $temp['name'];
move_uploaded_file($temp['tmp_name'], $filetowrite);
echo json_encode(array('location' => $filetowrite));
} else {
header("HTTP/1.0 500 Server Error");
}
我错过了什么吗?我在哪里可以找到"上传图片"编辑器中的按钮(我希望它出现在"插入/编辑图像"窗口中)。某处有一个有效的例子吗?
答案 0 :(得分:2)
我使用tinymce 4这里是我如何进行图片上传:
timymce config:
file_browser_callback: function(field_name, url, type, win) {
if(type=='image') $('#file_name').click(); $('#field_name').val(field_name);
}
请参阅:https://www.tinymce.com/docs/configure/file-image-upload/
表格:
<form enctype="multipart/form-data" id="form_file" style="width:0px; height:0px; overflow:hidden">
<input id="file_name" name="file" type="file" />
</form>
<input type="hidden" id="field_name" value="" />
我使用输入隐藏的field_name在弹出窗口中显示图像链接
javascript:
$('#file_name').change(function(){
var field_name = $('#field_name').val()
document.getElementById(field_name).value='';
var file = this.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
//Your validation
var field_name = $('#field_name').val()
var type = type.substring(0, 5);
if(type=='image') {
var formData = new FormData($('#form_file')[0]);
$.ajax({
url: "upload/upload.php",
type: "POST",
data: formData,
async: false,
success: function (msg) {
document.getElementById(field_name).value = 'http://www.domain.fr/upload/'+name;
},
cache: false,
contentType: false,
processData: false
});
} else {
alert('Le fichier doit etre une image')
}
});
php:
header('Access-Control-Allow-Origin: *');
if($_FILES['file']['type'] != "") {
$source = $_FILES['file']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(move_uploaded_file ($_FILES['file']['tmp_name'], $source)) {
echo $source ;
}
}