直接上代碼:
base64ToFile (base64Data, filename) {
// 將base64的數(shù)據(jù)部分提取出來
const parts = base64Data.split(';base64,');
const contentType = parts[0].split(':')[1];
const raw = window.atob(parts[1]);
// 將原始數(shù)據(jù)轉(zhuǎn)換為Uint8Array
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
// 使用Blob創(chuàng)建File對(duì)象
const blob = new Blob([uInt8Array], { type: contentType });
return new File([blob], filename, { type: contentType });
}
this.Quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, Delta) => {
if (node.tagName === "IMG" && node.src.indexOf("data:image/png;base64,") != -1) {
const file = this.base64ToFile(node.src, 'image.png')
upload({ file, filename: 'file' }).then(res => {
if (res.code == 200) {
document.querySelector('img[src="' + node.src + '"]').src = res.fileName
} else {
Delta.ops = [];
this.$message.error("圖片上傳失斃靼颉:" + res.msg);
this.$message.warning("消息內(nèi)容不支持圖片晒骇!");
}
});
}
return Delta;
});