Detailed description of using Rich Text Editor to upload images, text editor to upload images

Source: Internet
Author: User

Detailed description of using Rich Text Editor to upload images, text editor to upload images

Rich Text Editor uploads images

1. Import js of kindeditor

<script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/kindeditor-all-min.js"></script><script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/lang/zh_CN.js"></script>

2. Associate kindeditor with a text domain textarea, that is, use textarea to initialize a kindeditor object

ItemAddEditor = TAOTAO. createEditor ("# itemAddForm [name = desc]"); // initialize Category Selection and image Uploader TAOTAO. init ({fun: function (node) {TAOTAO. changeItemParam (node, "itemAddForm ");}});

3. Set the parameters to be uploaded

Var TT = TAOTAO = {// editor parameter kingEditorParams: {// specify the File Upload parameter name filePostName: "uploadFile", // specify the url of the file upload request. UploadJson: '/pic/upload', // upload type, which are image, flash, media, file dir: "image"}, init: function (data) {// initialize the Image Upload Component this. initPicUpload (data); // initialize and select the category component this. initItemCat (data) ;}, // initialize the Image Upload Component initPicUpload: function (data) {$ (". picFileUpload "). each (function (I, e) {var _ ele = $ (e); _ ele. siblings ("div. pics "). remove (); _ ele. after ('\ <div class = "pics"> \ <ul> </ul> \ </div>'); // if (data & data. pics) {var imgs = data. pics. split (","); for (var I in imgs) {if ($. trim (imgs [I]). length> 0) {_ ele. siblings (". pics "). find ("ul "). append ("<li> <a href = '" + imgs [I] + "'target =' _ blank '>  </a> </li> ");}}} // bind a click event to the upload image button $ (e ). click (function () {var form = $ (this ). parentsUntil ("form "). parent ("form"); // open the Image Upload window KindEditor. editor (TT. kingEditorParams ). loadPlugin ('multiimage', function () {var editor = this; editor. plugin. multiImageDialog ({clickFn: function (urlList) {var imgArray = []; KindEditor. each (urlList, function (I, data) {imgArray. push (data. url); form. find (". pics ul "). append ("<li> <a href = '" + data. url + "'target = '_ blank'>  </a> </li>") ;}); form. find ("[name = image]"). val (imgArray. join (","); editor. hideDialog ();}});});});});

Server code

Public Map uploadPicture (MultipartFile uploadFile) {Map resultMap = new HashMap <> (); try {// generate a new file name // remove the original file name String oldName = uploadFile. getOriginalFilename (); // generate a new file name // UUID. randomUUID (); String newName = IDUtils. genImageName (); newName = newName + oldName. substring (oldName. lastIndexOf (". "); String imagePath = new DateTime (). toString ("/yyyy/MM/dd"); // upload the image boolean result = FtpUtil. uploadFile (FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH, imagePath, newName, uploadFile. getInputStream (); System. out. println ("result =" + result); if (! Result) {resultMap. put ("error", 1); resultMap. put ("message", "File Upload Failed"); System. out. println ("hh"); return resultMap;} resultMap. put ("error", 0); resultMap. put ("url", IMAGE_BASE_PATH + imagePath + "/" + newName); return resultMap;} catch (IOException e) {resultMap. put ("error", 1); resultMap. put ("message", "File Upload exception"); return resultMap ;}}

Note: the format of the json string returned by the server is {error: 0 | 1, message | url}, where error is an integer and not a string. If the image is written as a string, it can be uploaded to the server normally, but it cannot be displayed in the browser.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.