【问题标题】:Image upload from local computer in wysiwyg以所见即所得的方式从本地计算机上传图像
【发布时间】:2012-01-25 22:17:35
【问题描述】:

如何将图像从本地计算机上传到 TinyMCE 的最佳方法是什么?我找到了IMCE,但它是否仅适用于drupal?我需要适用于 asp.net MVC3 的解决方案。然后我找到了这个TinySLUpload,但我想要没有silverlight的解决方案。我的选择是什么,哪个是最好的?

【问题讨论】:

    标签: asp.net-mvc image asp.net-mvc-3 tinymce wysiwyg


    【解决方案1】:

    !!!!享受!!!这是直接从本地计算机加载的解决方案

    JSFIDDLE DEMO

    `tinymce.init({
    selector: "textarea",
    toolbar: "mybutton",
    height:400,
    setup: function(editor) {
        editor.addButton('mybutton', {
            text:"IMAGE",
            icon: false,
            onclick: function(e) {
                console.log($(e.target));
                if($(e.target).prop("tagName") == 'BUTTON'){
                console.log($(e.target).parent().parent().find('input').attr('id'));                    if($(e.target).parent().parent().find('input').attr('id') != 'tinymce-uploader') {
                $(e.target).parent().parent().append('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
                    }
                $('#tinymce-uploader').trigger('click');
                $('#tinymce-uploader').change(function(){
                 var input, file, fr, img;
    
            if (typeof window.FileReader !== 'function') {
                write("The file API isn't supported on this browser yet.");
                return;
            }
    
            input = document.getElementById('tinymce-uploader');
            if (!input) {
                write("Um, couldn't find the imgfile element.");
            }
            else if (!input.files) {
                write("This browser doesn't seem to support the `files` property of file inputs.");
            }
            else if (!input.files[0]) {
                write("Please select a file before clicking 'Load'");
            }
            else {
                file = input.files[0];
                fr = new FileReader();
                fr.onload = createImage;
                fr.readAsDataURL(file);
            }
    
            function createImage() {
                img = new Image();
                img.src = fr.result;
                 editor.insertContent('<img src="'+img.src+'"/>');
    
            }
    
                });
    
            }
            if($(e.target).prop("tagName") == 'DIV'){
            if($(e.target).parent().find('input').attr('id') != 'tinymce-uploader') {
            console.log($(e.target).parent().find('input').attr('id'));                                
                $(e.target).parent().append('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
                             }
                $('#tinymce-uploader').trigger('click');
                $('#tinymce-uploader').change(function(){
                 var input, file, fr, img;
    
            if (typeof window.FileReader !== 'function') {
                write("The file API isn't supported on this browser yet.");
                return;
            }
    
            input = document.getElementById('tinymce-uploader');
            if (!input) {
                write("Um, couldn't find the imgfile element.");
            }
            else if (!input.files) {
                write("This browser doesn't seem to support the `files` property of file inputs.");
            }
            else if (!input.files[0]) {
                write("Please select a file before clicking 'Load'");
            }
            else {
                file = input.files[0];
                fr = new FileReader();
                fr.onload = createImage;
                fr.readAsDataURL(file);
            }
    
            function createImage() {
                img = new Image();
                img.src = fr.result;
                 editor.insertContent('<img src="'+img.src+'"/>');
    
            }
    
                });
    
            }
            if($(e.target).prop("tagName") == 'I'){
            console.log($(e.target).parent().parent().parent().find('input').attr('id')); if($(e.target).parent().parent().parent().find('input').attr('id') != 'tinymce-uploader') {               $(e.target).parent().parent().parent().append('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
                                                                                           }
                $('#tinymce-uploader').trigger('click');
                $('#tinymce-uploader').change(function(){
                 var input, file, fr, img;
    
            if (typeof window.FileReader !== 'function') {
                write("The file API isn't supported on this browser yet.");
                return;
            }
    
            input = document.getElementById('tinymce-uploader');
            if (!input) {
                write("Um, couldn't find the imgfile element.");
            }
            else if (!input.files) {
                write("This browser doesn't seem to support the `files` property of file inputs.");
            }
            else if (!input.files[0]) {
                write("Please select a file before clicking 'Load'");
            }
            else {
                file = input.files[0];
                fr = new FileReader();
                fr.onload = createImage;
                fr.readAsDataURL(file);
            }
    
            function createImage() {
                img = new Image();
                img.src = fr.result;
                 editor.insertContent('<img src="'+img.src+'"/>');
    
            }
    
                });
    
            }
    
            }
        });
    }});
    

    `

    【讨论】:

      【解决方案2】:

      上述直接在 TinyMCE 上上传图像的解决方案非常棒,事实上它对我来说非常有效。 insertContent 上的小问题,它没有显示我的图像,所以我做了一些改变。而不是使用 insertContent 替换它使用 editor.dom.createeditor.selection.setNode(image) 添加图像元素。并且不要忘记取消绑定 change 事件 以避免多次绑定可能导致重复新上传的图像取决于您已经上传了多少张图像。

      setup: function (editor) {
                  editor.addButton('mybutton', {
                      text: "Image",
                      icon: false,
                      onclick: function (e) {
                          if ($(e.target).prop("tagName") == 'BUTTON') {
                              if ($(e.target).parent().parent().find('input').attr('id') != 'tinymce-uploader') {
                                  $(e.target).parent().parent().append('<input id="tinymce-uploader" type="file" name="pic" accept="image/*" style="display:none">');
                              }
      
                              $('#tinymce-uploader').trigger('click');
      
                              $('#tinymce-uploader').change(changeImage);
      
                              function changeImage () {
                                  var input, file, fr, img;
      
                                  if (typeof window.FileReader !== 'function') {
                                      write("The file API isn't supported on this browser yet.");
                                      return;
                                  }
      
                                  input = document.getElementById('tinymce-uploader');
                                  if (!input) {
                                      write("Um, couldn't find the imgfile element.");
                                  }
                                  else if (!input.files) {
                                      write("This browser doesn't seem to support the `files` property of file inputs.");
                                  }
                                  else if (!input.files[0]) {
                                      write("Please select a file before clicking 'Load'");
                                  }
                                  else {
                                      file = input.files[0];
                                      fr = new FileReader();
                                      fr.onload = createImage;
                                      fr.readAsDataURL(file);
                                  }
      
                                  function createImage() {
                                      img = new Image();
                                      img.src = fr.result;                                    
                                      var image = editor.dom.create('img', { src: img.src }, "");
                                      editor.selection.setNode(image);
      
                                  }
                                  $('#tinymce-uploader').unbind("change", changeImage);
                              }
      
                          }                    
      
                      }
                  });
              }
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多