python djiango 文件上传
file = request.FILES.get('file')
u_folder = request.data.get('folder', 'default')
str2 = uuid.uuid4().hex
folder = str2[:2]
directory_name = datetime.datetime.now().strftime('%Y/%m/' + folder)
filename = str2[2:10] + '_' + str(random.randint(1000, 9999)) + os.path.splitext(file.name)[-1]
saved_path = os.path.join('media', u_folder, directory_name)
absolute_file_path = os.path.join(saved_path, filename) # file.name为带后缀的文件名
directory = os.path.dirname(absolute_file_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(absolute_file_path, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
ra_data = {
'name': file.name,
'path': '/' + absolute_file_path,
'type': file.content_type.lower()
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/17137597.html

浙公网安备 33010602011771号