主题: upload for python-django |
作者: linyupark, 发布日期: 2011-03-17 12:18:06, 浏览数: 5874 |
贴个上传处理的文件,针对python-django的,适用django1.2.5
# -*- coding:utf-8 -*- import json, os, datetime, time from django.conf import settings from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt # 2.5MB - 2621440 # 5MB - 5242880 # 10MB - 10485760 # 20MB - 20971520 # 50MB - 5242880 # 100MB 104857600 # 250MB - 214958080 # 500MB - 429916160 # # import 到具体views.py上 @csrf_exempt def ke_upload_view(request): ext_allowed = ['gif', 'jpg', 'jpeg', 'png'] max_size = 2621440 today = datetime.datetime.today() save_dir = 'upload/images/%d/%d/%d/' % (today.year, today.month, today.day) save_path = settings.MEDIA_ROOT+save_dir save_url = settings.MEDIA_URL+save_dir #print save_dir, save_path, save_url if request.method == 'POST': file = request.FILES['imgFile'] if not file.name: return HttpResponse(json.dumps( { 'error': 1, 'message': u'请选择要上传的文件' } )) ext = file.name.split('.').pop() if ext not in ext_allowed: return HttpResponse(json.dumps( { 'error': 1, 'message': u'请上传后缀为%s的文件' % ext_allowed} )) if file.size > max_size: return HttpResponse(json.dumps( { 'error': 1, 'message': u'上传的文件大小不能超过2.5MB'} )) if not os.path.isdir(save_path): os.makedirs(save_path) new_file = '%s.%s' % (int(time.time()), ext) destination = open(save_path+new_file, 'wb+') for chunk in file.chunks(): destination.write(chunk) destination.close() return HttpResponse(json.dumps( { 'error': 0, 'url': save_url+new_file} )) |
作者: 青怪, 发布日期: 2011-03-21 16:30:09 |
刚好用到。
|
回复 |
作者: deczealot, 发布日期: 2011-06-01 17:21:05 |
能具体说说怎么用吗?我刚刚解除ke,请多指教 |
回复 |
作者: 炸酱面, 发布日期: 2012-05-06 16:32:56 |
我还不知道把kindeditor集成到Django的admin里,能请教下吗?
|
回复 |