소개:
- 서식 있는 텍스트 편집기로 업로드된 이미지는 64비트로 디코딩되므로 이미지가 너무 길면 데이터베이스에서 오류를 보고하게 됩니다.
- 첫 번째 방법: 데이터베이스 유형을 다음으로 변경합니다.
mediumtext
- 두 번째 방법: 이 문서의 방법
이 기사에서 사용된 구문 설탕은 Vue3 설정 구문입니다.<script setup>
思路
- 리치 텍스트 편집기의 이미지 업로드 기능을 가로채십시오. 즉, 버튼을 클릭하여 이벤트를 업로드된 이미지 에 바인딩
input
하거나upload
- 이미지를 자신의 서버에 업로드하고 URL 주소를 반환하세요.
- URL 주소를 데이터베이스에 저장하고 표시할 때 직접 매핑합니다.
1단계: 설치quill-image-extend-module
npm install quill-image-extend-module --save-dev
파트 2: 글로벌 등록main.js
import {
QuillEditor,Quill } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import {
container,QuillWatch,ImageExtend} from "quill-image-extend-module";
Quill.register('modules/ImageExtend', ImageExtend)
const app = createApp(App)
app.component('QuillEditor', QuillEditor)
3단계: 구성 요소 참조(引入我的部分代码,不相关部分已去除)
<template>
<QuillEditor
v-model:content="form.productParaImgs"
contentType="html"
theme="snow"
ref="productParaImgsForm"
:options="productParaImgsEditorOption"
@change="onEditorChange($event)"
style="height:200px"
></QuillEditor>
<input
type="file"
accept=".png,.jpg,.jpeg"
@change="productParaImgsChange"
id="productParaImgsUpload"
style="display: none;border: 4px solid red"
/>
</template>
<script setup>
import {
container, ImageExtend, QuillWatch} from 'quill-image-extend-module'
const productParaImgsForm = ref(null);
const productParaImgsEditorOption = ref({
placeholder: "请输入",
theme: "snow",
modules: {
ImageExtend: {
name: 'img',
size: 3,
action: '',
response: (res) => {
},
headers: (xhr) => {
xhr.setRequestHeader('token', window.sessionStorage.getItem('token'))
},
},
toolbar: {
container: container,
handlers: {
'image': function (value) {
if (value) {
document.querySelector("#productParaImgsUpload").click();
} else {
this.quill.format("image", false);
}
}
}
}
}
})
const productParaImgsChange = async (e) => {
let formData = new FormData();
let file = e.target.files[0];
formData.append('file', file);
let result = await requestUitl.post("/sys/product/uploadImage", formData);
if (result.data.code == 647) {
let quill = productParaImgsForm.value.getQuill()
let length = quill.getSelection().index;
quill.insertEmbed(length, "image", getServerUrl() + result.data.src);
quill.setSelection(length + 1);
}
}
</script>
백엔드 반환 유형 참조
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("title", newFileName);
dataMap.put("src", "image/product/" + newFileName);
패키지 참조 요청
export function post(url, params = {
}) {
return new Promise((resolve, reject) => {
httpService({
url: url,
method: 'post',
data: params
}).then(response => {
resolve(response);
}).catch(error => {
reject(error);
});
});
}
export default {
post,
getServerUrl
}