# coding:utf-8 from django.http import HttpResponse from django.conf import settings from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required # 登录需求装饰器 import datetime,time,json,base64,os,uuid from app_doc.models import Image,ImageGroup,Attachment from app_admin.models import SysSetting import requests import random @login_required() @csrf_exempt def upload_ice_img(request): ################## # 如果需要使用ice自带的多文件上传,请修改ice的js文件中的附件上传部分代码如下: # # for(var i=0;i allow_img_size: return {"success": 0, "message": "图片大小超出{}MB".format(allow_img_size / 1048576)} relative_path = upload_generation_dir(dir_name) file_name = files.name.replace(file_suffix,'').replace('.','') + '_' +str(int(time.time())) + '.' + file_suffix path_file=os.path.join(relative_path, file_name) path_file = settings.MEDIA_ROOT + path_file # print('文件路径:',path_file) file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//",'/') # print("文件URL:",file_url) with open(path_file, 'wb') as f: for chunk in files.chunks(): f.write(chunk) # 保存文件 Image.objects.create( user=user, file_path=file_url, file_name=file_name, remark='本地上传', group = group_id, ) return {"success": 1, "url": file_url,'message':'上传图片成功'} # base64编码图片上传 def base_img_upload(files,dir_name, user): files_str = files.split(';base64,')[-1] # 截取图片正文 files_base = base64.b64decode(files_str) # 进行base64编码 relative_path = upload_generation_dir(dir_name) file_name = str(datetime.datetime.today()).replace(':', '').replace(' ', '_').split('.')[0] + '.png' # 日期时间 path_file = os.path.join(relative_path, file_name) path_file = settings.MEDIA_ROOT + path_file # print('文件路径:', path_file) file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/") # print("文件URL:", file_url) with open(path_file, 'wb') as f: f.write(files_base) # 保存文件 Image.objects.create( user = user, file_path = file_url, file_name=file_name, remark = '粘贴上传', ) return {"success": 1, "url": file_url, 'message': '上传图片成功'} # url图片上传 def url_img_upload(url,dir_name,user): relative_path = upload_generation_dir(dir_name) file_name = str(datetime.datetime.today()).replace(':', '').replace(' ', '_').split('.')[0] + '.png' # 日期时间 path_file = os.path.join(relative_path, file_name) path_file = settings.MEDIA_ROOT + path_file # print('文件路径:', path_file) file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/") # print("文件URL:", file_url) header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" } r = requests.get(url, headers=header, stream=True) if r.status_code == 200: with open(path_file, 'wb') as f: f.write(r.content) # 保存文件 Image.objects.create( user=user, file_path=file_url, file_name=file_name, remark='粘贴上传', ) resp_data = { 'msg': '', 'code': 0, 'data' : { 'originalURL': url, 'url': file_url } } return resp_data # return {"success": 1, "url": file_url, 'message': '上传图片成功'}