Python--上传文件和下载文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/12/28 13:56
# @Author : Aries
# @Site :
# @File : uploadFileQF.py
# @Software: PyCharm Community Edition
import os
from loggingutils.mylogger import logger as log
import time
# 上传
def handle_uploaded_file(f, interface_name):
base_path = os.getcwd()
file_name = ''
try:
path = base_path + r'\kyoto\kyoTo\uploadfile'\
+ '\\' + time.strftime('%Y%m%d')
if not os.path.exists(path):
os.makedirs(path)
file_name = path + '\\' + time.strftime('%Y%m%d%H%M%S') + interface_name + 'TestData.txt'
log.info('file_name : ' + file_name)
destination = open(file_name, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
except Exception:
log.error('write file error')
return file_name
# 下载(Django中返回对象用StreamingHttpResponse)
def download_file(filename, chunk_size=512):
with open(filename, 'rb') as f:
while True:
content = f.read(chunk_size)
if content:
yield content
else:
break
log.info('download operate success')
return content