欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用Flask框架實現(xiàn)文件上傳實例

 更新時間:2023年08月09日 10:10:45   作者:晚風吹兒  
這篇文章主要介紹了Python使用Flask庫文件上傳實例,用?Flask?處理文件上傳很容易,只要確保HTML表單中設置enctype="multipart/form-data"屬性就可以了,需要的朋友可以參考下

一、應用程序

已上傳的文件被儲存在內(nèi)存或文件系統(tǒng)的臨時位置。

可以通過請求對象 files 屬性來訪問上傳的文件。

每個上傳的文件都儲存在這個字典型屬性中。

這個屬性基本和標準 Python file 對象一樣,另外上傳文件保存到服務器的文件系統(tǒng)中,用 save() 方法

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        print(request.files)
        f.save(secure_filename(f.filename))
        return 'file uploaded successfully'
    else:
        return render_template('upload.html')
if __name__ == '__main__':
    app.run(debug = True)

二、html腳本

代碼如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:5000/upload" method="POST" enctype="multipart/form-data">
        <input type="file" name="file"  />
        <input type="submit" value="提交" />
    </form>
</body>
</html>

三、上傳結果

訪問路徑://localhost:5000/upload

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

到此這篇關于Python使用Flask框架實現(xiàn)文件上傳實例的文章就介紹到這了,更多相關Flask文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • pandas 缺失值與空值處理的實現(xiàn)方法

    pandas 缺失值與空值處理的實現(xiàn)方法

    這篇文章主要介紹了pandas 缺失值與空值處理的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • 最新評論