This code is the latest KindEditor 3.5.x to upload local images. The JSON format required for the KindEditor of the personal space to be revised by oschina is as follows:
{"Error": 0, "message": "...", "url": "/img/1111.gif "}
When the error value is 0, the upload is successful. You must specify the url value as the URL address after saving the image. If the error value is not 0, set the message value to the error message.
First, specify the URI of the upload process.
| The Code is as follows: |
Copy code |
KE. show ({ Id: 'ta _ blog_content ', ResizeMode: 1, ShadowMode: false, AllowPreviewEmoticons: false, UrlType: 'absolute ', AllowUpload: true, // allows image uploading ImageUploadJson: '/action/blog/upload_img' // the image processing URI uploaded by the server. }); |
Image Upload Processing Method
| The Code is as follows: |
Copy code |
/** * Upload images * @ Param ctx * @ Throws IOException */ @ Annotation. PostMethod @ Annotation. JSONOutputEnabled Public void upload_img (RequestContext ctx) throws IOException { File imgFile = ctx. image ("imgFile "); If (imgFile. length ()> MAX_IMG_SIZE ){ Ctx. output_json ( New String [] {"error", "message "}, New Object [] {1, ResourceUtils. getString ("error", "file_too_large", MAX_IMG_SIZE/1024 )} ); Return; } String uri = new SimpleDateFormat ("yyyyMMdd"). format (new Date ()) + "/IMG _" + RandomStringUtils. randomAlphanumeric (4) + '_' + String. valueOf (ctx. user (). getId ()) + '.' + FilenameUtils. getExtension (imgFile. getName (). toLowerCase (); Multimedia. saveImage (imgFile, img_path + uri, 0, 0 ); Ctx. output_json (new String [] {"error", "url"}, new Object [] {0, LinkTool. upload ("space/" + uri )}); } |
Kindeditor uploads local images.
Why is a server error reported when kindeditor uploads a local image?
In demo. jsp:
| The Code is as follows: |
Copy code |
KE. show ({ Id: 'content1 ', ImageUploadJson: '../jsp/upload_json.jsp ', FileManagerJson: '../jsp/file_manager_json.jsp'. Why are two levels of folders returned? ("../" Indicates that the first-level folder is returned) AllowFileManager: true, AfterCreate: function (id ){ KE. event. ctrl (document, 13, function (){ KE. util. setData (id ); Document. forms ['example ']. submit (); }); KE. event. ctrl (KE. g [id]. iframeDoc, 13, function (){ KE. util. setData (id ); Document. forms ['example ']. submit (); }); } });
|
It turns out that the JSON value of imageUploadJson is uploaded to the image folder under plugins. Therefore, to find upload_json.jsp, return to the root directory, that is, return two levels of menus. Now, we have finally solved the problem. We will start to transplant it to the article publishing system tomorrow, and finally we can go to bed.