簡(jiǎn)述
寫(xiě)一個(gè)簡(jiǎn)單的flask文件下載接口。
依賴(lài)
flask、gevent
代碼
不廢話上代碼。
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Oct 23 19:53:18 2021 @author: huyi """ from flask import Flask, request, make_response, send_from_directory from gevent.pywsgi import WSGIServer from gevent import monkey # 將python標(biāo)準(zhǔn)的io方法,都替換成gevent中的同名方法,遇到io阻塞gevent自動(dòng)進(jìn)行協(xié)程切換 monkey.patch_all() app = Flask(__name__) @app.route("/download", methods=['GET']) def download_file(): get_data = request.args.to_dict() file_path = get_data.get('fileName') response = make_response( send_from_directory('/Users/huyi/Movies/Videos',file_path,as_attachment=True)) response.headers["Content-Disposition"] = "attachment; filename={}".format( file_path.encode().decode('latin-1')) return response if __name__ == '__main__': WSGIServer(('0.0.0.0', 8080), app).serve_forever()
準(zhǔn)備數(shù)據(jù):
瀏覽器輸入:http://localhost:8080/download?fileName=test.mp4
下載完成。
總結(jié)
沒(méi)啥好總結(jié)的。
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。
到此這篇關(guān)于python 詳解如何寫(xiě)flask文件下載接口的文章就介紹到這了,更多相關(guān)python flask下載內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/120921334