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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM