發(fā)現(xiàn)問題:
用Vue插件quill-editor添加圖片的時(shí)候默認(rèn)會(huì)被轉(zhuǎn)為base64編碼導(dǎo)致數(shù)據(jù)很長(zhǎng),因此上傳數(shù)據(jù)的時(shí)候會(huì)發(fā)生錯(cuò)誤
問題解決:
1,創(chuàng)建quill-config.js文件
/*富文本編輯圖片上傳配置*/
const uploadConfig = {
action: process.env.VUE_APP_UPLOAD, // 必填參數(shù) 圖片上傳地址
methods: "POST", // 必填參數(shù) 圖片上傳方式
token: "", // 可選參數(shù) 如果需要token驗(yàn)證,假設(shè)你的token有存放在sessionStorage
name: "file", // 必填參數(shù) 文件的參數(shù)名
size: 2000, // 可選參數(shù) 圖片大小,單位為Kb, 1M = 1024Kb
accept: "image/png, image/gif, image/jpeg, image/bmp, image/x-icon", // 可選 可上傳的圖片格式
};
// toolbar工具欄的工具選項(xiàng)(默認(rèn)展示全部)
const toolOptions = [
["bold", "italic", "underline", "strike"],
["blockquote", "code-block"],
[{ header: 1 }, { header: 2 }],
[{ list: "ordered" }, { list: "bullet" }],
[{ script: "sub" }, { script: "super" }],
[{ indent: "-1" }, { indent: "+1" }],
[{ direction: "rtl" }],
[{ size: ["small", false, "large", "huge"] }],
[{ header: [1, 2, 3, 4, 5, 6, false] }],
[{ color: [] }, { background: [] }],
[{ font: [] }],
[{ align: [] }],
["clean"],
["link", "image", "video"],
];
const handlers = {
image: function image() {
var self = this;
var fileInput = this.container.querySelector("input.ql-image[type=file]");
if (fileInput === null) {
fileInput = document.createElement("input");
fileInput.setAttribute("type", "file");
// 設(shè)置圖片參數(shù)名
if (uploadConfig.name) {
fileInput.setAttribute("name", uploadConfig.name);
}
// 可設(shè)置上傳圖片的格式
fileInput.setAttribute("accept", uploadConfig.accept);
fileInput.classList.add("ql-image");
// 監(jiān)聽選擇文件
fileInput.addEventListener("change", function () {
// 創(chuàng)建formData
var formData = new FormData();
formData.append(uploadConfig.name, fileInput.files[0]);
formData.append("object", "product");
// 如果需要token且存在token
if (uploadConfig.token) {
formData.append("token", uploadConfig.token);
}
// 圖片上傳
var xhr = new XMLHttpRequest();
xhr.open(uploadConfig.methods, uploadConfig.action, true);
// 上傳數(shù)據(jù)成功,會(huì)觸發(fā)
xhr.onload = function (e) {
if (xhr.status === 200) {
var res = JSON.parse(xhr.responseText);
let length = self.quill.getSelection(true).index;
//這里很重要,你圖片上傳成功后,img的src需要在這里添加,res.path就是你服務(wù)器返回的圖片鏈接。
self.quill.insertEmbed(length, "image", res.data.url);
self.quill.setSelection(length + 1);
console.log("res.path === ", res.data.url);
}
fileInput.value = "";
};
// 開始上傳數(shù)據(jù)
xhr.upload.onloadstart = function (e) {
fileInput.value = "";
};
// 當(dāng)發(fā)生網(wǎng)絡(luò)異常的時(shí)候會(huì)觸發(fā),如果上傳數(shù)據(jù)的過程還未結(jié)束
xhr.upload.onerror = function (e) {};
// 上傳數(shù)據(jù)完成(成功或者失?。r(shí)會(huì)觸發(fā)
xhr.upload.onloadend = function (e) {
// console.log('上傳結(jié)束')
};
xhr.send(formData);
});
this.container.appendChild(fileInput);
}
fileInput.click();
},
};
export default {
placeholder: "",
theme: "snow", // 主題
modules: {
toolbar: {
container: toolOptions, // 工具欄選項(xiàng)
handlers: handlers, // 事件重寫
},
},
};
需要特別注意這一行,把服務(wù)器返回的URL賦值到富文本的img進(jìn)行展示
//把服務(wù)器返回的URL賦值到富文本的img進(jìn)行展示
self.quill.insertEmbed(length, "image", res.data.url);
2,Vue頁面使用
<script>中:
import quillConfig from '../article/quill-config.js'
export default {
name: "article",
data() {
return {
quillOption: quillConfig,
...
<template>中
<quill-editor
v-model="articleForm.articleContent"
ref="myQuillEditor"
:options="quillOption"
>
</quill-editor>