Codeigniter Codeiginter-do“上传”;“未选择文件”;

Codeigniter Codeiginter-do“上传”;“未选择文件”;,codeigniter,tinymce,image-uploading,Codeigniter,Tinymce,Image Uploading,我试图从所见即所得文本区(TinyMCE)上传图像,但它不工作,它给我错误“文件未选择” 我使用的是多部分表单,它与其他“输入文件”冲突吗 谢谢 这是我正在使用的代码 视图(.twig) Javascript function initImageUpload(editor) { // create input and insert in the DOM var inp = $('<input id="tinymce-uploader" type="file" name="p

我试图从所见即所得文本区(TinyMCE)上传图像,但它不工作,它给我错误“文件未选择”

我使用的是多部分表单,它与其他“输入文件”冲突吗

谢谢

这是我正在使用的代码

视图(.twig)

Javascript

function initImageUpload(editor) {
    // create input and insert in the DOM
    var inp = $('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
    $(editor.getElement()).parent().append(inp);

    // add the image upload button to the editor toolbar
    editor.addButton('imageupload', {
        text: '',
        icon: 'image',
        onclick: function(e) { // when toolbar button is clicked, open file select modal
        inp.trigger('click');
        }
    });

    // when a file is selected, upload it to the server
    inp.on("change", function(e){
        uploadFile($(this), editor);
    });
}

function uploadFile(inp, editor) {
    var input = inp.get(0);
    var data = new FormData();
    data.append('image[file]', input.files[0]);

    $.ajax({
        url: BASE_URL + 'admin/blog/upload_image_tinymce',
        type: 'POST',
        data: data,
        processData: false, // Don't process the files
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        success: function(data, textStatus, jqXHR) {
            console.log(data);
            editor.insertContent('<img class="content-img" src="' + data.url + '"/>');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if(jqXHR.responseText) {
                errors = JSON.parse(jqXHR.responseText).errors
                alert('Error uploading image: ' + errors.join(", ") + '. Make sure the file is an image and has extension jpg/jpeg/png.');
            }
        }
    });
}
tinymce.init({
    language: "pt_PT",
    language_url: BASE_URL + "/admin/js/pt_PT.js",
    selector: ".editor",
    height: 600,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link imageupload",
    toolbar2: "print preview media | forecolor backcolor emoticons",
    image_advtab: !0,

    setup: function(editor) {
        initImageUpload(editor);
    }
函数initImageUpload(编辑器){
//在DOM中创建输入并插入
var inp=$('');
$(editor.getElement()).parent().append(inp);
//将图像上载按钮添加到编辑器工具栏
editor.addButton('imageupload'{
文本:“”,
图标:“图像”,
onclick:function(e){//单击工具栏按钮时,打开文件选择模式
输入触发器(“点击”);
}
});
//选择文件后,将其上载到服务器
输入打开(“更改”,功能(e){
上载文件($(此),编辑器);
});
}
函数上传文件(inp,编辑器){
var输入=输入获取(0);
var data=new FormData();
data.append('image[file]”,input.files[0]);
$.ajax({
url:BASE\u url+“admin/blog/upload\u image\u tinymce”,
键入:“POST”,
数据:数据,
processData:false,//不处理文件
contentType:false,//将content-type设置为false,因为jQuery将告诉服务器它的查询字符串请求
成功:函数(数据、文本状态、jqXHR){
控制台日志(数据);
编者。插入内容(“”);
},
错误:函数(jqXHR、textStatus、errorshown){
if(jqXHR.responseText){
errors=JSON.parse(jqXHR.responseText).errors
警报('Error upload image:'+errors.join(“,”+”)。确保该文件是一个图像并具有扩展名jpg/jpeg/png');
}
}
});
}
tinymce.init({
语言:“pt_pt”,
语言:BASE\u url+“/admin/js/pt\u pt.js”,
选择器:“.editor”,
身高:600,
插件:[
“advlist autolink列出链接图像charmap打印预览hr锚定页面中断”,
“searchreplace wordcount visualblocks visualchars代码全屏显示”,
“insertdatetime媒体非中断保存表上下文菜单方向性”,
“表情模板粘贴文本颜色选择器文本模式”
],
工具栏1:“插入文件撤消重做|样式选择|粗体斜体|对齐左对齐中心对齐右对齐对齐对齐对齐|粗体numlist outdent缩进|链接图像上载”,
工具栏2:“打印预览媒体|前景色背景表情”,
图像\u advtab:!0,
设置:函数(编辑器){
initImageUpload(编辑器);
}

您在表单和tinyMce图像上载中使用了不同的索引。您的html表单有名为“image”的文件输入。但是您的MCE ajax文件上载名为“pic”。因此,在控制器函数中
upload\u image\u tinyMce()
$\u文件['image']
替换为
$\u文件['pic]

在控制器中使用此选项:

public function upload_image_tinymce() {
   //Check whether user upload picture
   if (!empty($_FILES['pic']['name'])) {
      $config['upload_path'] = ADDONPATH.'themes/escolamagica/img/escolamagica-blog/';
      $config['allowed_types'] = 'jpg|jpeg|png|gif';
      $config['file_name'] = $_FILES['pic']['name'];
      $config['overwrite'] = TRUE;
      $config['max_size'] = '10240';
      $config['max_width'] = '10000';
      $config['max_height'] = '10000';

      $this->load->library('upload', $config);
      $this->upload->initialize($config);

      if ($this->upload->do_upload('pic')) {
         $picture = str_replace('\\', '/', base_url().'img/escolamagica-blog/'.$_FILES['pic']['name']);
      } else {
        echo 'CONFIG';
        var_dump($config);

        echo 'IMAGE';
        var_dump($_FILES);

        echo 'ERROR';
        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
        die;

        $picture = '';
      }
   } else {
      $picture = '';
   }

   return $picture;
}
function initImageUpload(editor) {
    // create input and insert in the DOM
    var inp = $('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
    $(editor.getElement()).parent().append(inp);

    // add the image upload button to the editor toolbar
    editor.addButton('imageupload', {
        text: '',
        icon: 'image',
        onclick: function(e) { // when toolbar button is clicked, open file select modal
        inp.trigger('click');
        }
    });

    // when a file is selected, upload it to the server
    inp.on("change", function(e){
        uploadFile($(this), editor);
    });
}

function uploadFile(inp, editor) {
    var input = inp.get(0);
    var data = new FormData();
    data.append('image[file]', input.files[0]);

    $.ajax({
        url: BASE_URL + 'admin/blog/upload_image_tinymce',
        type: 'POST',
        data: data,
        processData: false, // Don't process the files
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        success: function(data, textStatus, jqXHR) {
            console.log(data);
            editor.insertContent('<img class="content-img" src="' + data.url + '"/>');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if(jqXHR.responseText) {
                errors = JSON.parse(jqXHR.responseText).errors
                alert('Error uploading image: ' + errors.join(", ") + '. Make sure the file is an image and has extension jpg/jpeg/png.');
            }
        }
    });
}
tinymce.init({
    language: "pt_PT",
    language_url: BASE_URL + "/admin/js/pt_PT.js",
    selector: ".editor",
    height: 600,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link imageupload",
    toolbar2: "print preview media | forecolor backcolor emoticons",
    image_advtab: !0,

    setup: function(editor) {
        initImageUpload(editor);
    }
public function upload_image_tinymce() {
   //Check whether user upload picture
   if (!empty($_FILES['pic']['name'])) {
      $config['upload_path'] = ADDONPATH.'themes/escolamagica/img/escolamagica-blog/';
      $config['allowed_types'] = 'jpg|jpeg|png|gif';
      $config['file_name'] = $_FILES['pic']['name'];
      $config['overwrite'] = TRUE;
      $config['max_size'] = '10240';
      $config['max_width'] = '10000';
      $config['max_height'] = '10000';

      $this->load->library('upload', $config);
      $this->upload->initialize($config);

      if ($this->upload->do_upload('pic')) {
         $picture = str_replace('\\', '/', base_url().'img/escolamagica-blog/'.$_FILES['pic']['name']);
      } else {
        echo 'CONFIG';
        var_dump($config);

        echo 'IMAGE';
        var_dump($_FILES);

        echo 'ERROR';
        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
        die;

        $picture = '';
      }
   } else {
      $picture = '';
   }

   return $picture;
}