Flask框架編寫文件下載接口過(guò)程講解
方式一
@app.route("/download1") def download(): # return send_file('test.exe', as_attachment=True) return send_file('2.jpg') # return send_file('1.mp3')
如果不加as_attachment參數(shù),則會(huì)向?yàn)g覽器發(fā)送文件,比如發(fā)送一張圖片:
發(fā)送mp3
加上as_attachment參數(shù),則會(huì)下載文件。
方式二
通過(guò)Response來(lái)實(shí)現(xiàn)
部分代碼如下:
@app.route("/download2") def download2(): file_name = "1.jpg" #https://www.runoob.com/http/http-content-type.html response = Response(file_send(file_name), content_type='image/jpeg') response.headers["Content-disposition"] = f'attachment; filename={file_name}' return response
其中content-type要根據(jù)文件的具體類型來(lái)設(shè)置,具體參考如下常見(jiàn)的:
content-type(內(nèi)容類型),一般是指網(wǎng)頁(yè)中存在的 content-type,用于定義網(wǎng)絡(luò)文件的類型和網(wǎng)頁(yè)的編碼,決定瀏覽器將以什么形式、什么編碼讀取這個(gè)文件,content-type 標(biāo)頭告訴客戶端實(shí)際返回的內(nèi)容的內(nèi)容類型。
常見(jiàn)的媒體格式類型如下:
- text/html : HTML格式
- text/plain :純文本格式
- text/xml : XML格式
- image/gif :gif圖片格式
- image/jpeg :jpg圖片格式
- image/png:png圖片格式
- application/xhtml+xml :XHTML格式
- application/xml: XML數(shù)據(jù)格式
- application/atom+xml :Atom XML聚合格式
- application/json: JSON數(shù)據(jù)格式
- application/pdf:pdf格式
- application/msword : Word文檔格式
- application/octet-stream : 二進(jìn)制流數(shù)據(jù)(如常見(jiàn)的文件下載)
- application/x-www-form-urlencoded : 中默認(rèn)的encType,form表單數(shù)據(jù)被編碼為key/value格式發(fā)送到服務(wù)器(表單默認(rèn)的提交數(shù)據(jù)的格式)
完整代碼
app.py
from flask import Flask,send_file,Response from flask_cors import CORS app = Flask(__name__) CORS(app,resources={r"/*": {"origins": "*"}},supports_credentials=True) @app.route("/download1") def download(): # return send_file('test.exe', as_attachment=True) # return send_file('2.jpg') return send_file('1.mp3') # filefold file # return send_from_directory('./', 'test.exe', as_attachment=True) # send big file def file_send(file_path): with open(file_path, 'rb') as f: while 1: data = f.read(20 * 1024 * 1024) # per 20M if not data: break yield data @app.route("/download2") def download2(): file_name = "1.jpg" response = Response(file_send(file_name), content_type='image/jpeg') response.headers["Content-disposition"] = f'attachment; filename={file_name}' return response if __name__ == '__main__': app.config['JSON_AS_ASCII'] = False app.run(debug=True)
到此這篇關(guān)于Flask框架編寫文件下載接口過(guò)程講解的文章就介紹到這了,更多相關(guān)Flask文件下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
跟老齊學(xué)Python之for循環(huán)語(yǔ)句
看這個(gè)標(biāo)題,有點(diǎn)匪夷所思嗎?為什么for是難以想象的呢?因?yàn)樵趐ython中,它的確是很常用而且很強(qiáng)悍,強(qiáng)悍到以至于另外一個(gè)被稱之為迭代的東西,在python中就有點(diǎn)相形見(jiàn)絀了。在別的語(yǔ)言中,for的地位從來(lái)沒(méi)有如同python中這么高的。2014-10-10python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)
這篇文章主要介紹了python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01關(guān)于Python中浮點(diǎn)數(shù)精度處理的技巧總結(jié)
雙精度浮點(diǎn)數(shù)(double)是計(jì)算機(jī)使用的一種數(shù)據(jù)類型,使用 64 位(8字節(jié)) 來(lái)存儲(chǔ)一個(gè)浮點(diǎn)數(shù)。下面這篇文章主要給大家總結(jié)介紹了關(guān)于Python中浮點(diǎn)數(shù)精度處理的技巧,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08keras實(shí)現(xiàn)VGG16方式(預(yù)測(cè)一張圖片)
這篇文章主要介紹了keras實(shí)現(xiàn)VGG16方式(預(yù)測(cè)一張圖片),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07