简体   繁体   English

使用CKEditor自定义文件浏览器并使用ASP.Net MVC上传

[英]Using CKEditor custom filebrowser and upload with ASP.Net MVC

I have a MVC app that Im trying to use CKEditor with. 我有一个试图与CKEditor一起使用的MVC应用程序。 One example I was looking at is here but there are many others. 我在看的一个例子在这里,但还有许多其他例子。 So far so good, but one section im still curious about, is the js that sends the selected file name back to the file upload dialog textbox. 到目前为止,到目前为止还不错,但我仍然对此感到好奇的是,将所选文件名发送回文件上传对话框文本框的js。

<script type="text/javascript">
    $(document).ready(function () {
        $(".returnImage").click("click", function (e) {
            var urlImage = $(this).attr("data-url");
            window.opener.updateValue("cke_72_textInput", urlImage);
            window.close();
        });
    });
</script>

In particular, the cke_72_textInput element. 特别是cke_72_textInput元素。 My example wasnt working initially, until I opened chrome dev tools and found the actual id of the textinput, which was in my case cke_76_textInput. 我的示例最初并未起作用,直到打开chrome dev工具并找到textinput的实际ID,在我的情况下为cke_76_textInput。 Why the id change I wonder? 我为什么想更改ID? Seems a little "fragile" to refer to a specific id like this? 似乎有点“脆弱”来指代这样的特定ID? The above js code just takes the selected image file and returns it into the textbox of the fileupload dialog. 上面的js代码仅获取选定的图像文件,并将其返回到fileupload对话框的文本框中。

Is there something exposed that references this textbox element indirectly without specifying it by id (via the config for example)? 是否有暴露的东西间接引用了此文本框元素,而没有通过id指定它(例如,通过config)?

I had the same problem...a little frustrating that I couldn't find any official documentation, considering this seems like a common use case. 我遇到了同样的问题...令人沮丧的是我找不到任何官方文档,考虑到这似乎是一个常见的用例。

Anyways, take a look at the quick tutorial here: http://r2d2.cc/2010/11/03/file-and-image-upload-with-asp-net-mvc2-with-ckeditor-wysiwyg-rich-text-editor/ . 无论如何,请在此处查看快速教程: http : //r2d2.cc/2010/11/03/file-and-image-upload-with-asp-net-mvc2-with-ckeditor-wysiwyg-rich-text -editor / In case the link ever breaks, here's what I did. 万一链接断开,这就是我所做的。

    [HttpPost]
    public ActionResult UploadImage(HttpPostedFileBase upload, string ckEditorFuncNum)
    {
        /* 
           add logic to upload and save image here
        */

        var path = "~/Path/To/image.jpg"; // Logical relative path to uploaded image
        var url = string.Format("{0}://{1}{2}", 
            Request.Url.Scheme, 
            Request.Url.Authority, 
            Url.Content(path)); // URL path to uploaded image
        var message = "Saved!"; // Optional

        var output = string.Format("<script>window.parent.CKEDITOR.tools.callFunction({0}, '{1}', '{2}');</script>", 
            CKEditorFuncNum, 
            url, 
            message);
        return Content(output);
    }

On view: 在视图上:

$(document).ready(function () {   

 CKEDITOR.replace('Text-area-name', {
            filebrowserImageUploadUrl: '/Controller-name/UploadImage'
        });

        CKEDITOR.editorConfig = function (config) {
            // Define changes to default configuration here. For example: 
            config.language = 'de';
            // config.extraPlugins = 'my_own_plugin'; // if you have any plugin 
            // config.uiColor = '#AADC6E'; 
            // config.image_previewText = CKEDITOR.tools.repeat(' Hier steht dann dein guter Text. ', 8 ); 
            // config.contentsLanguage = 'de'; 
            config.height = 350; // 350px, specify if you want a larger height of the editor 

            config.linkShowAdvancedTab = false;
            config.linkShowTargetTab = false;
        };

 CKEDITOR.on('dialogDefinition', function (ev) {
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
            ev.data.definition.resizable = CKEDITOR.DIALOG_RESIZE_NONE;

            if (dialogName == 'link') {
                var infoTab = dialogDefinition.getContents('info');
                infoTab.remove('protocol');
                dialogDefinition.removeContents('target');
                dialogDefinition.removeContents('advanced');
            }

            if (dialogName == 'image') {
                dialogDefinition.removeContents('Link');
                dialogDefinition.removeContents('advanced');
                var infoTab = dialogDefinition.getContents('info');
                infoTab.remove('txtBorder');
                infoTab.remove('txtHSpace');
                infoTab.remove('txtVSpace');
                infoTab.remove('cmbAlign');
            }
        });

    }

On Contoller: 在Contoller上:

 [HttpPost]
    public ActionResult UploadImage(HttpPostedFileBase file, string CKEditorFuncNum, string CKEditor, string langCode)
    {
     if (file.ContentLength <= 0)
            return null;

        // here logic to upload image
        // and get file path of the image

        const string uploadFolder = "Assets/img/";

        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath(string.Format("~/{0}", uploadFolder)), fileName);
        file.SaveAs(path);

        var url = string.Format("{0}{1}/{2}/{3}", Request.Url.GetLeftPart(UriPartial.Authority),
            Request.ApplicationPath == "/" ? string.Empty : Request.ApplicationPath,
            uploadFolder, fileName);

        // passing message success/failure
        const string message = "Image was saved correctly";

        // since it is an ajax request it requires this string
        var output = string.Format(
            "<html><body><script>window.parent.CKEDITOR.tools.callFunction({0}, \"{1}\", \"{2}\");</script></body></html>",
            CKEditorFuncNum, url, message);

        return Content(output);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM