当前位置:网站首页>Wangeditor rich text editor to upload pictures and solve cross-domain problems
Wangeditor rich text editor to upload pictures and solve cross-domain problems
2022-07-31 06:24:00 【God of data protection!】
I. Introduction
wangeditor rich text editor is a rich text editor developed by js and css, which is lightweight, concise and efficient
For more information, please check the official website: http://www.wangeditor.com/
Second, upload pictures
This chapter mainly talks about uploading pictures to the server using js language for saving
1. First stepCall the official website static resources2. Step 2const {createEditor,createToolbar}=window.wangEditor;//Create instance objectconst editorConfig={MENU_CONF:{},withCredentials:true,//Define this property as true to allow cross-domain accessautofocus: false,scroll: false,maxLength: 1200,minLength: 200,};editorConfig.MENU_CONF['uploadImage']={//This method is triggered when uploading or pasting an image to the editorfieldName:'image',server:'http://192.168.178.44:8888/upImage',//Background server addressmaxFileSize: 6 * 1024 * 1024, //maxNumberOfFiles: 200,// allowedFileTypes: ['image/*'],// timeout: 20 * 1000, // 5 seconds};
When we upload an image on the front end, the editor will upload the image to the backend server for saving. My address is 'http://192.168.178.44:8888/upImage', and the return image address will be echoed on the editor.The returned data must start with the following structure (upload success, upload failure), otherwise an error will be reported.
{"errno": 0, // Note: the value is a number, not a string"data": {"url": "xxx", // image src , must"alt": "yyy", // image description text, not required"href": "zzz" // link to the image, not required}}
{"errno": 1, // as long as not equal to 0"message": "Failure message"}
Three, server-side method implementation
@[email protected]@RequestMapping(value = "/upImage")public Object upImage(MultipartFile image) {String filePath = "D:\\IntelliJ IDEA 2019.3.3\\IDEA project\\retry\\src\\main\\resources\\static\\picture\\";String suffix = "";try {String originalFilename = image.getOriginalFilename();if (originalFilename.contains(".")) {suffix = originalFilename.substring(originalFilename.lastIndexOf("."));}String fileName=System.currentTimeMillis()+suffix;File targetFile = new File(filePath, fileName);if(!targetFile.exists()){targetFile.createNewFile();}try {image.transferTo(targetFile);} finally {}String url="http://localhost:8888/static/"+fileName;JSONObject jsonObject=new JSONObject();JSONObject jsonObject1=new JSONObject();System.out.println(url);jsonObject1.put("url",url);jsonObject.put("errno",0);jsonObject.put("data",jsonObject1);return jsonObject;} catch (Exception e) {JSONObject jsonObject=new JSONObject();jsonObject.put("errno",1);jsonObject.put("message","failure information");return jsonObject;}}
The CrossOrigin annotation indicates that cross-domain access is allowed
Four. Test
A background service needs to be started to use the editor
Image pasted successfully!
Go to the server to check if the image exists locally
Frontier knowledge is not easy to make!
This concludes this chapter!Good luck to you all!
边栏推荐
- VS通过ODBC连接MYSQL(二)
- MySQL 出现 The table is full 的解决方法
- random.randint函数用法
- CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
- Pytorch每日一练——预测泰坦尼克号船上的生存乘客
- 用pytorch里的children方法自定义网络
- 活体检测PatchNet学习笔记
- cv2.resize()是反的
- Natural language processing related list
- wangeditor富文本编辑器上传图片以及跨域问题解决
猜你喜欢
随机推荐
Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物
JS写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数JS
VS通过ODBC连接MYSQL(一)
mPEG-DSPE 178744-28-0 甲氧基-聚乙二醇-磷脂酰乙醇胺线性PEG磷脂
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
MySQL 免安装版的下载与配置教程
Understanding of objects and functions in js
break and continue exit in js
DC-CDN学习笔记
Podspec automatic upgrade script
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
Hyperparameter Optimization - Excerpt
Xiaomi mobile phone SMS location service activation failed
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
活体检测CDCN学习笔记
Cholesterol-PEG-DBCO 胆固醇-聚乙二醇-二苯基环辛炔化学试剂
Markdown help documentation
DSPE-PEG-Biotin,CAS:385437-57-0,磷脂-聚乙二醇-生物素可延长循环半衰期
Cholesterol-PEG-NHS NHS-PEG-CLS 胆固醇-聚乙二醇-活性酯可修饰小分子材料