您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
要使用 Flask 处理文件上传,请按照以下步骤操作:
pip install Flask
pip install Werkzeug
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file part', 400
file = request.files['file']
if file.filename == '':
return 'No selected file', 400
if file:
# 保存文件到服务器的某个目录
file.save('uploaded_files/' + file.filename)
return 'File uploaded and saved.', 200
if __name__ == '__main__':
app.run(debug=True)
templates
的文件夹,并在其中创建一个名为 index.html
的 HTML 文件,内容如下:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload with Flask</title>
</head>
<body>
<h1>Upload a File</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
python app.py
现在,你可以访问 http://localhost:5000/
并尝试上传一个文件。上传成功后,文件将保存在服务器的 uploaded_files
目录中。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。