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