# coding:utf-8 from utils import FileSize MF = FileSize("36723678") print(MF)
def UploadFile(request): """上传文件""" if not request.method == "POST": return HttpResponse(json.dumps(u"{'state:'ERROR'}"), content_type="application/javascript") state = "SUCCESS" action = request.GET.get("action") #上传文件 upload_field_name = { "uploadfile": "fileFieldName", "uploadimage": "imageFieldName", "uploadscrawl": "scrawlFieldName", "catchimage": "catcherFieldName", "uploadvideo": "videoFieldName", } UploadFieldName = request.GET.get( upload_field_name[action], USettings.UEditorUploadSettings.get(action, "upfile")) #上传涂鸦,涂鸦是采用base64编码上传的,需要单独处理 if action == "uploadscrawl": upload_file_name = "scrawl.png" upload_file_size = 0 else: #取得上传的文件 file = request.FILES.get(UploadFieldName, None) if file is None: return HttpResponse(json.dumps(u"{'state:'ERROR'}"), content_type="application/javascript") upload_file_name = file.name upload_file_size = file.size #取得上传的文件的原始名称 upload_original_name, upload_original_ext = os.path.splitext( upload_file_name) #文件类型检验 upload_allow_type = { "uploadfile": "fileAllowFiles", "uploadimage": "imageAllowFiles", "uploadvideo": "videoAllowFiles" } if upload_allow_type.has_key(action): allow_type = list( request.GET.get( upload_allow_type[action], USettings.UEditorUploadSettings.get(upload_allow_type[action], ""))) if not upload_original_ext in allow_type: state = u"服务器不允许上传%s类型的文件。" % upload_original_ext #大小检验 upload_max_size = { "uploadfile": "filwMaxSize", "uploadimage": "imageMaxSize", "uploadscrawl": "scrawlMaxSize", "uploadvideo": "videoMaxSize" } max_size = long( request.GET.get( upload_max_size[action], USettings.UEditorUploadSettings.get(upload_max_size[action], 0))) if max_size != 0: from utils import FileSize MF = FileSize(max_size) if upload_file_size > MF.size: state = u"上传文件大小不允许超过%s。" % MF.FriendValue #检测保存路径是否存在,如果不存在则需要创建 upload_path_format = { "uploadfile": "filePathFormat", "uploadimage": "imagePathFormat", "uploadscrawl": "scrawlPathFormat", "uploadvideo": "videoPathFormat" } path_format_var = { "basename": upload_original_name, "extname": upload_original_ext[1:], "filename": upload_file_name, "time": datetime.datetime.now().strftime("%H%M%S"), "datetime": datetime.datetime.now().strftime("%Y%m%d%H%M%S"), "rnd": random.randrange(100, 999) } #取得输出文件的路径 OutputPathFormat, OutputPath, OutputFile = get_output_path( request, upload_path_format[action], path_format_var) #所有检测完成后写入文件 if state == "SUCCESS": if action == "uploadscrawl": state = save_scrawl_file(request, os.path.join(OutputPath, OutputFile)) else: #保存到文件中,如果保存错误,需要返回ERROR state = save_upload_file(file, os.path.join(OutputPath, OutputFile)) #返回数据 return_info = { 'url': urllib.basejoin(USettings.gSettings.MEDIA_URL, OutputPathFormat), # 保存后的文件名称 'original': upload_file_name, #原始文件名 'type': upload_original_ext, 'state': state, #上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中 'size': upload_file_size } return HttpResponse(json.dumps(return_info, ensure_ascii=False), content_type="application/javascript")
def UploadFile(request, uploadtype, uploadpath): if not request.method == "POST": return HttpResponse(simplejson.dumps(u"{'state:'ERROR'}"), mimetype="Application/javascript") state = "SUCCESS" file = request.FILES.get("upfile", None) #如果没有提交upfile则返回错误 if file is None: return HttpResponse(simplejson.dumps(u"{'state:'ERROR'}"), mimetype="Application/javascript") #取得上传的文件的原始名称 original_name, original_ext = file.name.split('.') #类型检验 if uploadtype == "image" or uploadtype == "scrawlbg": allow_type = USettings.UEditorSettings["images_upload"]['allow_type'] else: allow_type = USettings.UEditorSettings["files_upload"]['allow_type'] if not original_ext in allow_type: state = u"服务器不允许上传%s类型的文件。" % original_ext #大小检验 max_size = USettings.UEditorSettings["images_upload"]['max_size'] if max_size != 0: from utils import FileSize MF = FileSize(max_size) if file.size > MF.size: state = u"上传文件大小不允许超过%s。" % MF.FriendValue #检测保存路径是否存在,如果不存在则需要创建 OutputPath = os.path.join(USettings.gSettings.MEDIA_ROOT, os.path.dirname(uploadpath)).replace("//", "/") if not os.path.exists(OutputPath): os.makedirs(OutputPath) #要保存的文件名格式使用"原文件名_当前时间.扩展名" OutputFile = GenerateRndFilename(file.name) #所有检测完成后写入文件 if state == "SUCCESS": #保存到文件中 state = SaveUploadFile(file, os.path.join(OutputPath, OutputFile)) #返回数据 if uploadtype == "image" or uploadtype == "scrawlbg": rInfo = { 'url': OutputFile, #保存后的文件名称 'title': request.POST.get("pictitle", file.name), #文件描述,对图片来说在前端会添加到title属性上 'original': file.name, #原始文件名 'state': state #上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中 } else: rInfo = { 'url': OutputFile, #保存后的文件名称 'original': file.name, #原始文件名 'filetype': original_ext, 'state': state #上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中 } if uploadtype == "scrawlbg": #上传涂鸦背景 return HttpResponse( u"<script>parent.ue_callback('%s','%s');</script>" % (rInfo["url"], rInfo["state"])) else: #上传文件与图片 return HttpResponse(simplejson.dumps(rInfo), mimetype="application/javascript")