错误 将文件上传到我选择的文件夹时,会出现以下错误。
由于我是不熟悉Flask的人,因此无法捕获错误。
未找到
在服务器上找不到请求的URL。如果您手动输入网址,请检查拼写,然后重试。
app.py
/**
* Custom credentials to validate the status of user.
*/
public function credentials(Request $request)
{
return [
'email' => $request->email,
'password' => $request->password,
'is_active' => '1'
];
}
upload.html
import os
#import magic
import urllib.request
#from app import app
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename
app = Flask(__name__)
#app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
UPLOAD_FOLDER = '/static'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the files part
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File(s) successfully uploaded')
return redirect('/')
if __name__ == "__main__":
app.run(debug = True)
答案 0 :(得分:1)
您应该更改代码
@app.route('/', methods=['POST', 'GET'])
def upload_file():
if request.method == 'POST':
# check if the post request has the files part
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File(s) successfully uploaded')
return redirect('/')
elif request.method == 'GET':
return render_template('upload.html')
答案 1 :(得分:0)
这里是upload.html:-
<form action="/upload" method="POST" enctype="multipart/form-data">
{{ file_form.hidden_tag() }}
{{ file_form.file }}
{{ file_form.submit }}
</form>
WTForms :-
from flask_wtf import FlaskForm
from wtforms import SubmitField, FileField
class FileForm(FlaskForm):
file = FileField('File')
submit = SubmitField('submit')
数据库模型:-
class Image(db.Model):
sno = db.Column(db.Integer, primary_key=True,)
fileName = db.Column(db.String(50), nullable=False)
app.py :-
@app.route("/upload", methods = ['GET', 'POST'])
def upload():
if ('user' in session and session['user'] == (Admin.query.filter_by(name=session['user']).first()).name):
file_form = FileForm()
if file_form.validate_on_submit():
f = file_form.file.data
print(f.filename)
if f.filename == "":
flash('no file')
if allowed_image(f.filename):
if os.path.isfile(f'{app.config["UPLOAD_FOLDER"]}/{f.filename}'):
flash(f'file {f.filename} already have in server')
else:
try:
f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename)))
if os.path.isfile(f'{app.config["UPLOAD_FOLDER"]}/{f.filename}'):
image = Image(fileName=str(f.filename))
db.session.add(image)
db.session.commit()
else:
os.unlink(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.fileName)))
flash('could not added file in db , so file removed')
redirect('/dashbord')
flash('Image successfully uploaded')
# redirect('/dashbord')
except IOError:
flash('Image could not be uploaded')
return redirect('/dashbord')
else:
flash('That file extension is not allowed')
return redirect('/dashbord')
return redirect('/dashbord')