python3 flask實(shí)現(xiàn)文件上傳功能
更新時(shí)間:2020年03月20日 09:20:00 作者:diyiday
這篇文章主要為大家詳細(xì)介紹了python3 flask實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python3-flask文件上傳操作的具體代碼,供大家參考,具體內(nèi)容如下
# -*- coding: utf-8 -*- import os import uuid import platform from flask import Flask,request,redirect,url_for from werkzeug.utils import secure_filename if platform.system() == "Windows": slash = '\\' else: platform.system()=="Linux" slash = '/' UPLOAD_FOLDER = 'upload' ALLOW_EXTENSIONS = set(['html', 'htm', 'doc', 'docx', 'mht', 'pdf']) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER #判斷文件夾是否存在,如果不存在則創(chuàng)建 if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) else: pass # 判斷文件后綴是否在列表中 def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1] in ALLOW_EXTENSIONS @app.route('/',methods=['GET','POST']) def upload_file(): if request.method =='POST': #獲取post過來的文件名稱,從name=file參數(shù)中獲取 file = request.files['file'] if file and allowed_file(file.filename): # secure_filename方法會(huì)去掉文件名中的中文 filename = secure_filename(file.filename) #因?yàn)樯洗蔚奈募赡苡兄孛虼耸褂胾uid保存文件 file_name = str(uuid.uuid4()) + '.' + filename.rsplit('.', 1)[1] file.save(os.path.join(app.config['UPLOAD_FOLDER'],file_name)) base_path = os.getcwd() file_path = base_path + slash + app.config['UPLOAD_FOLDER'] + slash + file_name print(file_path) return redirect(url_for('upload_file',filename = file_name)) return ''' <!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form action="" method=post enctype=multipart/form-data> <p><input type=file name=file> <input type=submit value=Upload> </form> ''' if __name__ == "__main__": app.run(host='0.0.0.0',port=5000)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python count函數(shù)使用方法實(shí)例解析
這篇文章主要介紹了Python count函數(shù)使用方法實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03對(duì)python中字典keys,values,items的使用詳解
今天小編就為大家分享一篇對(duì)python中字典keys,values,items的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python內(nèi)置函數(shù)hex()的實(shí)現(xiàn)示例
這篇文章主要介紹了Python內(nèi)置函數(shù)hex()的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python 3實(shí)戰(zhàn)爬蟲之爬取京東圖書的圖片詳解
最近在學(xué)習(xí)python3,下面這篇文章主要給大家介紹了關(guān)于Python3實(shí)戰(zhàn)爬蟲之爬取京東圖書圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-10-10python常量折疊基礎(chǔ)知識(shí)點(diǎn)講解
在本篇文章里小編給大家整理的是一篇關(guān)于python常量折疊基礎(chǔ)知識(shí)點(diǎn)講解,對(duì)此有興趣的朋友可以跟著學(xué)習(xí)下。2021-02-02python爬蟲自動(dòng)創(chuàng)建文件夾的功能
這篇文章主要介紹了python爬蟲自動(dòng)創(chuàng)建文件夾的功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08