In flask request.form["input_name"] is used to get the input data, but not for input type=files
which are accesible through request.files["input_name"], always using enctype=multipart/form-data
in the form. You can get more info in the oficial documentation:
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
On the other hand, request.files['filename'] is a FileStorage type, the function open(f)
expects str, bytes or os.PathLike object, not FileStorage.
The following code should works:
from flask import render_template, request, redirect
from app import app
import os
import csv
@app.route('/', methods=["GET", "POST"])
def index():
data = []
if request.method == 'POST':
if request.files:
uploaded_file = request.files['filename'] # This line uses the same variable and worked fine
filepath = os.path.join(app.config['FILE_UPLOADS'], uploaded_file.filename)
uploaded_file.save(filepath)
with open(filepath) as file:
csv_file = csv.reader(file)
for row in csv_file:
data.append(row)
return redirect(request.url)
return render_template('index.html', data=data)
@app.route('/help')
def help():
return render_template('help.html')
app.config['FILE_UPLOADS'] = "C:\Users\Zachary\Documents\VSCode_Projects\monday_webapp\app\static\file\uploads"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…