summernote自定义上传图片接口
<script> var $summernote = $('.summernote').summernote({ height: 350, placeholder: '请勿发布违规内容', callbacks: { onImageUpload: function(files) { summernoteUpload(files); }, onChange: function(contents, $editable) { $('#content').val(contents) } } }); function summernoteUpload(files){ var formData = new FormData(); for(var i = 0; i < files.length; i++){ formData.append('file', files[i]); $.ajax({ url:"/upload", type:"post", data: formData, contentType: false, processData: false, success: function(e) { if(e.code == 200){ let imgNode = document.createElement("img"); imgNode.setAttribute('src', '/storage/' + e.data[0]) $summernote.summernote('insertNode', imgNode); }else{ toastr.error(e.msg); } }, error:function(data) { toastr.error('上传失败'); } }); } } </script>
try { $a = validate([ 'file'=>'fileExt:jpg,jpeg,gif,png' ])->check(['file' => $files]); $savename = []; // 上传到本地服务器 foreach($files as $val){ $path = \think\facade\Filesystem::disk('public')->putFile('topic', $val); $path = str_replace('\\', '/', $path); $savename[] = $path; } } catch (\think\exception\ValidateException $e) { return json(['code' => 400, 'msg' => $e->getMessage()]); } return json(['code' => 200, 'msg' => 'ok', 'data' => $savename]);
0 条评论
登陆后才能评论哦~