1. 依赖
pnpm install @wangeditor/editor --save
pnpm install @wangeditor/editor-for-vue@next --save
2. 在template使用wangEditor 5
v-model
数据库中查询出来的editor中的数据,数据库中使用longtext
类型
< Toolbarstyle = " border-bottom : 1px solid #ccc" :editor = " editorRef" :mode = " mode" /> < Editorstyle = " height : 500px; overflow-y : hidden; " v-model = " content" :mode = " mode" :defaultConfig = " editorConfig" @onCreated = " handleCreated" />
3. js代码
customInsert
:这个配置的作用,发送图片到接口返回的只有图片的文件名,但是要在editor中显示图片需要图片完整的url,这里就是把url补完整
import '@wangeditor/editor/dist/css/style.css'
import { onBeforeUnmount, reactive, shallowRef} from "vue" ;
import { Editor, Toolbar} from '@wangeditor/editor-for-vue'
const editorRef = shallowRef ( )
const mode = 'default'
const editorConfig = { MENU_CONF : { } }
editorConfig. MENU_CONF [ 'uploadImage' ] = { headers : { token : '' , } , server : import . meta. env. VITE_BASE_API + "/avatar/wang/upload" , fieldName : 'file' , customInsert : ( res, insertFn ) => { const baseUrl = import . meta. env. VITE_BASE_API ; const imageUrl = res. data[ 0 ] . url; const newImageUrl = baseUrl + imageUrl; insertFn ( newImageUrl) ; } ,
}
onBeforeUnmount ( ( ) => { const editor = editorRef. valueif ( editor == null ) return editor. destroy ( )
} )
const handleCreated = ( editor ) => { editorRef. value = editor
}
4. 后端接口
@RequestMapping ( "/download/{imgPath}" ) public void download ( @PathVariable ( "imgPath" ) String fileName, HttpServletResponse response) { String UPLOAD_PATH = System. getProperty ( "user.dir" ) + "/avatar/" + fileName; FileUtilCustom. downloadFile ( UPLOAD_PATH , response) ; } @PostMapping ( "/wang/upload" ) public Map< String, Object> wangEditorUpload ( MultipartFile file ) { String UPLOAD_PATH = System. getProperty ( "user.dir" ) + "/avatar/" ; String fileName = FileUtilCustom. uploadFile ( file, UPLOAD_PATH ) ; Map< String, Object> resMap = new HashMap < > ( ) ; resMap. put ( "errno" , 0 ) ; resMap. put ( "data" , CollUtil. newArrayList ( Dict. create ( ) . set ( "url" , "/avatar/download/" + fileName) ) ) ; return resMap; }
接口中调用的上传和下载的方法
import cn. hutool. core. io. FileUtil;
import com. example. exception. CustomException;
import jakarta. servlet. http. HttpServletResponse;
import org. springframework. web. multipart. MultipartFile; import java. io. OutputStream;
import java. net. URLEncoder;
import java. nio. charset. StandardCharsets;
import java. util. UUID ; public class FileUtilCustom { public static String uploadFile ( MultipartFile file, String filePath ) { if ( file. isEmpty ( ) ) { return "文件为空" ; } if ( ! FileUtil. isDirectory ( filePath) ) { FileUtil. mkdir ( filePath) ; } try { String fileName = UUID . randomUUID ( ) . toString ( ) . substring ( 0 , 10 ) + '_' + file. getOriginalFilename ( ) ; FileUtil. writeBytes ( file. getBytes ( ) , filePath + fileName) ; return fileName; } catch ( Exception e) { throw new CustomException ( "文件上传失败" ) ; } } public static void downloadFile ( String filePath, HttpServletResponse response ) { if ( ! FileUtil. exist ( filePath) ) { throw new CustomException ( "文件不存在" ) ; } try { String fileName = FileUtil. getName ( filePath) ; String encodeName = URLEncoder. encode ( fileName, StandardCharsets. UTF_8 ) ; response. setHeader ( "Content-Disposition" , "attachment'filename=" + encodeName ) ; response. setContentType ( "application/octet-stream" ) ; OutputStream outputStream = response. getOutputStream ( ) ; FileUtil. writeToStream ( filePath, outputStream) ; outputStream. close ( ) ; } catch ( Exception e) { throw new CustomException ( "文件下载失败" ) ; } }
}