通常我们在开发时,需要限制前端用户上传文件的格式类型,但在某些特殊情况下,我们希望能支持用户上传任意格式的附件文件。使用UEditor时,我们可以通过指定后端配置参数fileAllowFiles的值来设置编辑器允许上传的附件类型,该参数设置为空数组时,前端上传所有文件均提示格式不支持。
于是,我们需要手动修改一下UEditor的代码,找到并打开ueditor/dialog/attachment/attachment.js文件,搜索“acceptExtensions”字符串,找到两处代码:
第一处是
acceptExtensions = (editor.getOpt('fileAllowFiles') || []).join('').replace(/\./g, ',').replace(/^[,]/, '');;
第二处是
if (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1) {
showError('not_allow_type');
uploader.removeFile(file);
}
将第二处代码修改为:
if (acceptExtensions!='' && (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1)) {
showError('not_allow_type');
uploader.removeFile(file);
}
这样,当fileAllowFiles设置为空数组时(即不指定限制格式),前端就可以上传任意格式的附件了。
另外需要提醒的是,后端接收文件的代码需要自己根据需要验证文件格式是否符合要求。
本文介绍了如何修改UEditor的代码以允许用户上传任意格式的附件,即使在`fileAllowFiles`配置参数为空数组时。主要涉及修改`attachment.js`文件中关于`acceptExtensions`的判断条件,当不指定文件格式限制时,前端不再阻止任何格式的文件上传。同时,强调后端仍需进行文件格式的验证以确保安全性。

2592

被折叠的 条评论
为什么被折叠?



