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

文件上传与下载
本文介绍了一个使用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


转载于:https://www.cnblogs.com/fqfanqi/p/8204160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值