KindEditor is an open source online HTML editor written in Javascript. Its main user is to allow users to obtain visible and available editing effects on the website. Developers can use KindEditor to enter traditional multi-line text input boxes. (textarea) is replaced with a visual rich text input box
to download the latest version of KindEditor from the official website, unzip and copy all files to the static of the project folder. Note that you can delete the following directories as needed:
<script src="{% static 'kindeditor-4.1.10/kindeditor-all-min.js' %}"></script><script> var options = { resizeType: 0, uploadJson: "/upload/", //指定图片上传的url路径,server端写一个视图来处理上传的图片 extraFileUploadParams: { "csrfmiddlewaretoken": "{{ csrf_token }}" } }; KindEditor.create("#article_body", options); //为指定元素创建富文本编辑器</script>
uploadJson parameter. The url path
extraFileUploadParams parameters are used to submit csrf verification
src attribute of the
tag displays the preview image
from django.conf.urls import urlfrom app import views urlpatterns = [ url(r'^upload/$', views.upload_file, name='upload_file'), url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), # 配置media路由]
def upload_file(request): # 拿到文件,保存在指定路径 print(request.FILES) # {'imgFile': [<InMemoryUploadedFile: QQ图片20170523192846.jpg (image/jpeg)>]} imgFile = request.FILES.get('imgFile') # 拿到文件对象,imgFile.name, 拿到文件名 with open('app01/media/upload/img/'+imgFile.name,'wb')as f: # with open 无法创建文件夹,需要自己创建 for chunk in imgFile.chunks(): f.write(chunk) # 返回json响应 response = { 'error': 0, 'url': '/media/upload/img/'+imgFile.name # 客户端拿到路径,才能预览图片; media在setting中配置了别名,这里只写media,客户端就可以找到路径,前面不需要加/app } return HttpResponse(json.dumps(response))
Description:
Get the image object
Get the file name
and url
The above is the detailed content of kindeditor image upload function. For more information, please follow other related articles on the PHP Chinese website!