Share via


Enable image upload for CKEditor

Question

Monday, May 6, 2013 8:46 AM

Hello,

I'm using a CKEditor at my asp.net project. I want to enable uploading images for user. I searched alot and used various methods but each of them has a problem. at one of them I used the method described at http://mycodingerrors.blogspot.com/2012/02/image-upload-with-ckeditor.html , the upload tab appeared but when upload image it isn't viewable and is like X. I think something at config .ascx should change for base url and ... but I don't know what should I do.  don't know where the uploaded images save. I set the url at ascx file like this:

<%@ Control Language="C#" EnableViewState="false" AutoEventWireup="false" Inherits="CKFinder.Settings.ConfigFile" %>
<%@ Import Namespace="CKFinder.Settings" %>
<script runat="server">

    /**
     * This function must check the user session to be sure that he/she is
     * authorized to upload and access files using CKFinder.
     */
    public override bool CheckAuthentication()
{
return true;
}

// Perform additional checks for image files.


    /**
     * All configuration settings must be defined here.
     */
    public override void SetConfig()
    {
        // Paste your license name and key here. If left blank, CKFinder will
        // be fully functional, in Demo Mode.
        LicenseName = "";
        LicenseKey = "";

        // The base URL used to reach files in CKFinder through the browser.
        BaseUrl = "";

        // The phisical directory in the server where the file will end up. If
        // blank, CKFinder attempts to resolve BaseUrl.
        BaseDir = "";

        // Optional: enable extra plugins (remember to copy .dll files first).
        Plugins = new string[] {
            // "CKFinder.Plugins.FileEditor, CKFinder_FileEditor",
            // "CKFinder.Plugins.ImageResize, CKFinder_ImageResize",
            // "CKFinder.Plugins.Watermark, CKFinder_Watermark"
        };
        // Settings for extra plugins.
        PluginSettings = new Hashtable();
        PluginSettings.Add("ImageResize_smallThumb", "90x90" );
        PluginSettings.Add("ImageResize_mediumThumb", "120x120" );
        PluginSettings.Add("ImageResize_largeThumb", "180x180" );
        // Name of the watermark image in plugins/watermark folder
        PluginSettings.Add("Watermark_source", "logo.gif" );
        PluginSettings.Add("Watermark_marginRight", "5" );
        PluginSettings.Add("Watermark_marginBottom", "5" );
        PluginSettings.Add("Watermark_quality", "90" );
        PluginSettings.Add("Watermark_transparency", "80" );

        // Thumbnail settings.
        // "Url" is used to reach the thumbnails with the browser, while "Dir"
        // points to the physical location of the thumbnail files in the server.
        Thumbnails.Url = BaseUrl + "_thumbs/";
        if ( BaseDir != "" ) {
            Thumbnails.Dir = BaseDir + "_thumbs/";
        }
        Thumbnails.Enabled = true;
        Thumbnails.DirectAccess = false;
        Thumbnails.MaxWidth = 100;
        Thumbnails.MaxHeight = 100;
        Thumbnails.Quality = 80;

        // Set the maximum size of uploaded images. If an uploaded image is
        // larger, it gets scaled down proportionally. Set to 0 to disable this
        // feature.
        Images.MaxWidth = 1600;
        Images.MaxHeight = 1200;
        Images.Quality = 80;

        // Indicates that the file size (MaxSize) for images must be checked only
        // after scaling them. Otherwise, it is checked right after uploading.
        CheckSizeAfterScaling = true;

        // Increases the security on an IIS web server.
        // If enabled, CKFinder will disallow creating folders and uploading files whose names contain characters
        // that are not safe under an IIS 6.0 web server.
        DisallowUnsafeCharacters = true;

        // If CheckDoubleExtension is enabled, each part of the file name after a dot is
        // checked, not only the last part. In this way, uploading foo.php.rar would be
        // denied, because "php" is on the denied extensions list.
        // This option is used only if ForceSingleExtension is set to false.
        CheckDoubleExtension = true;

        // Due to security issues with Apache modules, it is recommended to leave the
        // following setting enabled. It can be safely disabled on IIS.
        ForceSingleExtension = true;

        // For security, HTML is allowed in the first Kb of data for files having the
        // following extensions only.
        HtmlExtensions = new string[] { "html", "htm", "xml", "js" };

        // Folders to not display in CKFinder, no matter their location. No
        // paths are accepted, only the folder name.
        // The * and ? wildcards are accepted.
        // By default folders starting with a dot character are disallowed.
        HideFolders = new string[] { ".*", "CVS" };

        // Files to not display in CKFinder, no matter their location. No
        // paths are accepted, only the file name, including extension.
        // The * and ? wildcards are accepted.
        HideFiles = new string[] { ".*" };

        // Perform additional checks for image files.
        SecureImageUploads = true;

        // The session variable name that CKFinder must use to retrieve the
        // "role" of the current user. The "role" is optional and can be used
        // in the "AccessControl" settings (bellow in this file).
        RoleSessionVar = "CKFinder_UserRole";

        // ACL (Access Control) settings. Used to restrict access or features
        // to specific folders.
        // Several "AccessControl.Add()" calls can be made, which return a
        // single ACL setting object to be configured. All properties settings
        // are optional in that object.
        // Subfolders inherit their default settings from their parents' definitions.
        //
        //  - The "Role" property accepts the special "*" value, which means
        //    "everybody".
        //  - The "ResourceType" attribute accepts the special value "*", which
        //    means "all resource types".
        AccessControl acl = AccessControl.Add();
        acl.Role = "*";
        acl.ResourceType = "*";
        acl.Folder = "/";

        acl.FolderView = true;
        acl.FolderCreate = true;
        acl.FolderRename = true;
        acl.FolderDelete = true;

        acl.FileView = true;
        acl.FileUpload = true;
        acl.FileRename = true;
        acl.FileDelete = true;

        // Resource Type settings.
        // A resource type is nothing more than a way to group files under
        // different paths, each one having different configuration settings.
        // Each resource type name must be unique.
        // When loading CKFinder, the "type" querystring parameter can be used
        // to display a specific type only. If "type" is omitted in the URL,
        // the "DefaultResourceTypes" settings is used (may contain the
        // resource type names separated by a comma). If left empty, all types
        // are loaded.

        // ==============================================================================
        // ATTENTION: Flash files with `swf' extension, just like HTML files, can be used
        // to execute JavaScript code and to e.g. perform an XSS attack. Grant permission
        // to upload `.swf` files only if you understand and can accept this risk.
        // ==============================================================================

        DefaultResourceTypes = "";

        ResourceType type;

        type = ResourceType.Add( "Files" );
        type.Url = BaseUrl + "files/";
        type.Dir = BaseDir == "" ? "" : BaseDir + "files/";
        type.MaxSize = 0;
        type.AllowedExtensions = new string[] { "7z", "aiff", "asf", "avi", "bmp", "csv", "doc", "docx", "fla", "flv", "gif", "gz", "gzip", "jpeg", "jpg", "mid", "mov", "mp3", "mp4", "mpc", "mpeg", "mpg", "ods", "odt", "pdf", "png", "ppt", "pptx", "pxd", "qt", "ram", "rar", "rm", "rmi", "rmvb", "rtf", "sdc", "sitd", "swf", "sxc", "sxw", "tar", "tgz", "tif", "tiff", "txt", "vsd", "wav", "wma", "wmv", "xls", "xlsx", "zip" };
        type.DeniedExtensions = new string[] { };

        type = ResourceType.Add( "Images" );
        type.Url = BaseUrl + "Images/";
        type.Dir = BaseDir == "" ? "" : BaseDir + "Images/";
        type.MaxSize = 10000000;
        type.AllowedExtensions = new string[] { "bmp", "gif", "jpeg", "jpg", "png" };
        type.DeniedExtensions = new string[] { };

        type = ResourceType.Add( "Flash" );
        type.Url = BaseUrl + "flash/";
        type.Dir = BaseDir == "" ? "" : BaseDir + "flash/";
        type.MaxSize = 0;
        type.AllowedExtensions = new string[] { "swf", "flv" };
        type.DeniedExtensions = new string[] { };
    }

</script>

can anyone help me.

All replies (5)

Monday, May 6, 2013 12:56 PM ✅Answered

In order to do this, add the currentFolder attribute to the query string:

CKEDITOR.replace( 'editor1',
{
    filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
    filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',
    filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',
    filebrowserUploadUrl : 
       '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files&currentFolder=/archive/',
    filebrowserImageUploadUrl : 
       '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images&currentFolder=/cars/',
    filebrowserFlashUploadUrl : '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'
});

see CKEditor Configuration

already resolved here http://forums.asp.net/t/1851986.aspx/1

Regards.


Wednesday, May 8, 2013 11:56 AM ✅Answered

finally I could find the solution.

I did two things to solve my problem.

first I edited config.ascx file and set the base url to images folder. like below:

public override void SetConfig()
    {
        

        // The base URL used to reach files in CKFinder through the browser.
        BaseUrl = "~/images/";
        // The phisical directory in the server where the file will end up. If
        // blank, CKFinder attempts to resolve BaseUrl.
        BaseDir = "";
    }

and the mistake that I had was that, my pages was at admin folder and I was put ckeditor and ckfinder folder at root of my website.

by puting these files at the admin folder the problem soved.

thank you.


Monday, May 6, 2013 2:31 PM

I used the code but the image did not shown again. 

I want to upload the images to Images folder at my website's root. where should I set this url to put uploaded images there.

<textarea cols="80" id="editor1" name="editor1" rows="10"></textarea>

   <script type="text/javascript">
//<![CDATA[

       CKEDITOR.replace('editor1',
{
    filebrowserBrowseUrl: 'ckeditor/ckfinder/ckfinder.html',
    filebrowserImageBrowseUrl: 'ckeditor/ckfinder/ckfinder.html?type=Images',
    filebrowserFlashBrowseUrl: 'ckeditor/ckfinder/ckfinder.html?type=Flash',
    filebrowserUploadUrl:
           'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files&currentFolder=/archive/',
    filebrowserImageUploadUrl:
           'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images&currentFolder=/cars/',
    filebrowserFlashUploadUrl: 'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'
});
//]]>
   </script>

Monday, May 6, 2013 2:48 PM

Did you follow all instruction in following article?

http://docs.cksource.com/CKFinder_2.x/Developers_Guide/ASP.NET/CKEditor_Integration

where should I set this url to put uploaded images there.

filebrowserUploadUrl : 
       '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files&currentFolder=/archive/',
    filebrowserImageUploadUrl : 
       '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images&currentFolder=/cars/',

Regards.


Tuesday, May 7, 2013 2:16 AM

I did all the instructions at that link and I set filebrowserUploadUrl and filebrowserImageUploadUrl to Images folder like below. but the image didn't show and when I slick the upload button the error Image source url is missing shows and when I click send it to the server the image didn't show.

 <CKEditor:CKEditorControl ID="CKEditor1" BasePath="ckeditor/" runat="server">
   </CKEditor:CKEditorControl>
   <script type="text/javascript">
//<![CDATA[

       CKEDITOR.replace('CKEditor1',
{
    filebrowserBrowseUrl: 'ckeditor/ckfinder/ckfinder.html',
    filebrowserImageBrowseUrl: 'ckeditor/ckfinder/ckfinder.html?type=Images',
    filebrowserFlashBrowseUrl: 'ckeditor/ckfinder/ckfinder.html?type=Flash',
    filebrowserUploadUrl:
           'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files&currentFolder=/Images/',
    filebrowserImageUploadUrl:
           'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images&currentFolder=/Images/',
    filebrowserFlashUploadUrl: 'ckeditor/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'
});
//]]>
   </script>

should I change anything at ckfinder/config.ascx file for basepath and other things?