. NET core CKEditor picture uploads

Source: Internet
Author: User

Recently playing with ASP. NET core, do not want to use Ueditor, want to using CKEditor. Therefore, the image upload function is required.

Don't say much nonsense, first on:

CKEditor Front-End code:

    <textID= "Content"name= "Content"></text>    <Script>Ckeditor.replace ('content'); </Script>

CKEditor config.js Configuration Code: Need to configure image upload path

function (config) {    //  Define changes to the default configuration here. For example:    //  config.language = ' fr ';    // config.uicolor = ' #AADC6E ';    Config.basehref = "http://" + location.host;     = '/upload/uploadimage ';     = ' arial/arial; blackbody/blackbody; italics/italics; young/young round; Microsoft Jache/Microsoft Ya-black; ' + config.font_names;     true ;};

As the code above, we use Uploadcontroller's Uploadimage method to handle upload events.

Service-Side code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMICROSOFT.ASPNETCORE.MVC;usingMicrosoft.AspNetCore.Hosting;usingSystem.IO;usingRatuo.common;namespaceratuo.web.controllers{ Public classUploadcontroller:controller {Privateihostingenvironment _env;  PublicUploadcontroller (ihostingenvironment env) {_env=env; }         Public AsyncTask<iactionresult>Uploadimage () {stringcallback = request.query["Ckeditorfuncnum"];//Request return value            varUpload = request.form.files[0]; stringTPL ="<script type=\ "text/javascript\" >window.parent.ckeditor.tools.callfunction (\ "{1}\", \ "{0}\", \ "{2}\"); </script>"; if(Upload = =NULL)                returnContent (string. Format (TPL,"", Callback,"Please select a picture! "),"text/html"); //determine if it is a picture typelist<string> imgtypelist =Newlist<string> {"Image/pjpeg","Image/png","Image/x-png","Image/gif","image/bmp" }; if(Imgtypelist. FindIndex (x=>x = = Upload. ContentType) = =-1)            {                returnContent (string. Format (TPL,"", Callback,"please upload a picture! "),"text/html"); }            vardata = request.form.files["Upload"]; stringfilepath = _env. webrootpath+"\\userfile\\images"; stringImgname = Utils.getordernum () +utils.getfileextname (upload.            FileName); stringFullPath =Path.Combine (filepath, imgname); Try            {                if(!directory.exists (filepath))                Directory.CreateDirectory (filepath); if(Data! =NULL)                {                    awaitTask.run (() =                    {                        using(FileStream fs =NewFileStream (FullPath, FileMode.Create)) {data.                        CopyTo (FS);                }                    }); }            }            Catch(Exception ex) {returnContent (string. Format (TPL,"", Callback,"image upload failed:"+ ex. Message),"text/html"); }            returnContent (string. Format (TPL,"/userfile/images/"+ Imgname, Callback,""),"text/html"); }    }}

Compile and preview. Can!

. NET core CKEditor picture uploads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.