tornado實現文件下載的代碼
獲取請求參數;請求參數生成json格式,存入文件;下載json文件
class SpockDataIntegrationDownloadHandler(tornado.web.RequestHandler):
def post(self):
selectname = self.get_argument('selectname')
json_string = {}
"""
將請求參數放到dict中
"""
type = self.get_argument('type')
starttime = self.get_argument('starttime')
endtime = self.get_argument('end_time')
json_string['starttime'] = starttime
json_string['endtime'] = endtime
json_string['type'] = type
"""
生成json文件
"""
if json_string:
filepath = './jsonfile.conf'
if os.path.exists(filepath):
os.remove(filepath)
ff = open(filepath, 'w')
json.dump(json_string, ff) # 將json格式數據寫入文件
ff.close()
"""
下載文件
"""
filename = "jsonfile.conf"
self.set_header ('Content-Type', 'application/octet-stream')
self.set_header ('Content-Disposition', 'attachment; filename=' + filename)
buf_size = 4096
with open(os.path.join('',filepath), 'rb') as f:
while True:
data = f.read(buf_size)
if not data:
break
self.write(data)
self.finish()</pre>
本文由用戶 cf35 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!