Jodit-Angular编辑器截图上传:自定义序列号文件名问题求助
Jodit编辑器粘贴截图自定义「image+序列号」文件名问题解决
针对你使用Jodit 3.5.4 + jodit-angular 1.11.1时,粘贴截图无法自定义文件名的问题,结合旧版本特性,提供以下可行方案:
方案1:在粘贴阶段重写文件名
粘贴截图时,Jodit会生成临时Blob文件,可通过beforePaste事件直接修改文件名:
// Angular组件内的Jodit配置 this.joditConfig = { uploader: { url: '你的自定义上传URL' }, events: { beforePaste: (editor, event) => { const clipboardData = event.clipboardData || window.clipboardData; if (!clipboardData?.items) return; // 维护序列号计数器 let serialCounter = 0; const getNextSerial = () => { serialCounter++; return serialCounter; }; Array.from(clipboardData.items).forEach((item, index) => { if (item.type.includes('image')) { const file = item.getAsFile(); if (file) { const ext = file.name.split('.').pop() || 'png'; const newFileName = `image${getNextSerial()}.${ext}`; // 创建新文件替换原剪贴板项 const newFile = new File([file], newFileName, { type: file.type }); clipboardData.items[index] = new ClipboardItem({ [newFile.type]: newFile }); } } }); } } };
方案2:强制走Jodit上传流程处理文件名
旧版本中粘贴截图可能默认走原生上传逻辑,需禁用后触发自定义文件名处理:
this.joditConfig = { uploader: { url: '你的自定义上传URL', processInNative: false, // 禁用原生上传处理 processFileName: (originalName) => { // 这里可根据需求维护全局/局部序列号 static serialCounter = 0; serialCounter++; const ext = originalName.split('.').pop() || 'png'; return `image${serialCounter}.${ext}`; } }, saveSameFileNameStrategy: 'addNumber' };
方案3:确保配置完整传递到Jodit实例
检查Angular模板中是否完整传递配置对象,避免零散配置丢失:
<jodit-editor [config]="joditConfig"></jodit-editor>
组件内需保证joditConfig是包含所有上传、事件配置的完整对象,而非拆分设置。
内容的提问来源于stack exchange,提问作者Ginny Yang
相关产品推荐
相关产品推荐

