Summernote没有这个选项来控制扩展名,它接受各种图像,但我需要释放以pdf上传的文件,并找到解决方案检查:
在 Summernote.js 的主文件中查找以下行:
'type = "file" name = "files" accept = "image / *" multiple = "multiple" />' +
并将accept =“image / *”更改为您想要的格式
前任:
' type="file" name="files" accept="image/gif,image/jpeg,image/jpg,image/png,application/pdf,application/vnd.ms-excel,application/msword, application/vnd.ms-powerpoint,text/plain" multiple="multiple" />' +
希望这可以帮助
对于那些需要完整代码的人来说,除了图像之外,服务器还通过 Summernote 发送 pdf 文件、doc、txt 等文件,如下所示:
Js code
$ (document) .ready (function () {
(summernote) summernote ({
toolbar: [
['myotherbutton', ['parameters']],
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'clear'
['fontname', ['fontname']],
['fontsize', ['fontsize']],
['color', ['color']]
['for', ['ul', 'ol', 'paragraph']]
['table', ['table']],
['insert', ['link', 'picture', 'video', 'hr', 'codeview']]
],
lang: 'pt-BR',
height: 300,
minHeight: null,
maxHeight: null,
callbacks: {
onImageUpload: function (files) {
uploadFile (files [0]);
},
onMediaDelete: function (target) {
alert (target [0] .src)
deleteFile (target [0] .src);
}
}
});
function uploadFile (file) {
data = new FormData ();
data.append ("file", file);
$ .ajax ({
date: date,
type: "POST",
url: "../midia/upload.php", // replace with your url
cache: false,
contentType: false,
processData: false,
success: function (url) {
alert (url);
function checkURL (url) {
return (url.match (/ \. (jpeg | jpg | gif | png) $ /)! = null);
}
if (checkURL (url)) {
var linkfile = url.split ("Images /");
alert (linkfile [1]);
$ image = "../midia/Images/"+linkfile[1];
$ ('# publisher'). summernote ("insertImage", $ image);
} else {
var linkfile = url.split ("Images /");
$ ('# publisher'). summernote ('createLink', {
text: linkfile [1],
url: url,
isNewWindow: true
});
}
}
});
}
function deleteFile (src) {
$ .ajax ({
date: {src: src},
type: "POST",
url: "../midia/delete.php", // replace with your url
cache: false,
success: function (resp) {
console.log (resp);
}
});
}
});
将 php 文件发送到文件夹 /media/
上传.php
<? php
$ allowed = array ('png', 'jpg', 'gif', 'pdf', 'doc', 'docx', 'txt', 'mp3');
if (isset ($ _ FILES ['file']) && $ _FILES ['file'] ['error'] == 0) {
$ extension = pathinfo ($ _FILES ['file'] ['name'], PATHINFO_EXTENSION);
if (! in_array (strtolower ($ extension), $ allowed)) {
echo 'AN ERROR OCCURED - INVALID IMAGE';
exit;
}
if (move_uploaded_file ($ _FILES ['file'] ['tmp_name'], '../midia/Images/'.$_FILES['file']['name'])) {
echo $ _SERVER ['SERVER_NAME']. dirname ($ _ SERVER ['PHP_SELF']). '/ Images /'.$_ FILES [' file '] [' name ']; // echo absolute file_url
exit;
}
}
echo 'AN ERROR OCCURED';
exit;
?>
删除.php
<? php
$ url = explode ("Images", $ _ POST ['src']);
$ file_name = str_replace ($ url [0], '', $ _ POST ['src']); // striping host to get relative path
if (unlink ($ file_name))
{
echo 'file deleted!';
}
?>