def createDesignAttach(self,pid,filelist,operator): try: project = Project.objects.get(pk=pid) except Project.DoesNotExist : return None url_path = settings.UPLOAD_ATTACH_URL+"/" #路径获取 if('UPLOAD_PATH' in dir(settings) and 'UPLOAD_ATTACH_URL' in dir(settings)): path = settings.UPLOAD_PATH + url_path else: path = "~/" for file_tmp in filelist: now = time.strftime("%Y-%m-%d %X", time.localtime()) #上传 #获取扩展 splite_name = os.path.splitext(file_tmp._name) ext = '' ran = '_' + str(random.randint(1000,9999)) if len(splite_name) > 1: ext = splite_name[-1] new_name = str(int(time.time()))+ ran + ext.lower() upload_res = upload(file_tmp, path, new_name, "wb+") if upload_res: model = DesignAttach(project=project,name=file_tmp._name,path=settings.STATIC_URL+url_path+new_name,status=0) model.save() return
def createDesignAttach(self, pid, filelist, operator): try: project = Project.objects.get(pk=pid) except Project.DoesNotExist: return None url_path = settings.UPLOAD_ATTACH_URL + "/" #路径获取 if ('UPLOAD_PATH' in dir(settings) and 'UPLOAD_ATTACH_URL' in dir(settings)): path = settings.UPLOAD_PATH + url_path else: path = "~/" for file_tmp in filelist: now = time.strftime("%Y-%m-%d %X", time.localtime()) #上传 #获取扩展 splite_name = os.path.splitext(file_tmp._name) ext = '' ran = '_' + str(random.randint(1000, 9999)) if len(splite_name) > 1: ext = splite_name[-1] new_name = str(int(time.time())) + ran + ext.lower() upload_res = upload(file_tmp, path, new_name, "wb+") if upload_res: model = DesignAttach(project=project, name=file_tmp._name, path=settings.STATIC_URL + url_path + new_name, status=0) model.save() return
def add(request): #跳转列表 if request.method == 'GET' : return HttpResponseRedirect("/profile") file_tmp = request.FILES['img_file'] if 'img_file' in request.FILES else None content = request.REQUEST.get('content', None) content = stringHandler.post_content_replace(content) if not file_tmp and not content: return render(request, 'profile/index.html', {'msg':"请输入内容"}) upload_res = True new_name = None if file_tmp: #路径获取 if('UPLOAD_PATH' in dir(settings)): path = settings.UPLOAD_PATH else: path = "~/" #上传 #获取扩展 splite_name = os.path.splitext(file_tmp._name) ext = '' if len(splite_name) > 1: ext = splite_name[-1] new_name = str(int(time.time())) + ext.lower() upload_res = upload(file_tmp, path, new_name, "wb+") if upload_res: res = ProfileService.create(request.user.id, content, 0, new_name) if res: return HttpResponseRedirect("/profile") # return HttpResponse({"status":0}) else: return render(request, 'profile/index.html', {'msg':"上传失败"})