当前位置:网站首页>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!
边栏推荐
猜你喜欢

JS写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数JS

Tencent Cloud GPU Desktop Server Driver Installation

学习JDBC之获取数据库连接的方式

变分自编码器VAE实现MNIST数据集生成by Pytorch

Tensorflow边用边踩坑

VS2017 connects to MYSQL

Pytorch常用函数

RuntimeError: CUDA error: no kernel image is available for execution on the device问题记录

mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation

Nmap的下载与安装
随机推荐
Flutter mixed development module dependencies
Tensorflow相关list
Redis-Hash
Talking about the understanding of CAP in distributed mode
MySQL master-slave switching steps
Understanding of js arrays
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
应用usb_cam同时打开多个摄像头方法
This in js points to the prototype object
Navicat从本地文件中导入sql文件
Shell/Vim相关list
understand js operators
活体检测CDCN学习笔记
Redis-哈希
Cholesterol-PEG-NHS NHS-PEG-CLS cholesterol-polyethylene glycol-active ester can modify small molecular materials
Global scope and function scope in js
DSPE-PEG-COOH CAS:1403744-37-5 磷脂-聚乙二醇-羧基脂质PEG共轭物
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
Xiaomi mobile phone SMS location service activation failed
Notes on creating a new virtual machine in Hyper-V