未找到烧瓶上传文件

时间:2018-07-08 19:07:53

标签: python flask

我想使用Python,Flask和HTML制作文件上传器。如果上传文件,我得到:

  

未找到

     

在服务器上找不到请求的URL。如果输入URL   手动,请检查您的拼写,然后重试。

import os
from flask import Flask, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = './user/Tom/Files/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == ['POST']:
    if 'file' not in request.files:
        flash('Geen Bestand')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('Geen Bestand')
        return redirect(request.url)
    if not os.path.exists("user/Tom/Files/"):
        os.makedirs("user/Tom/Files/")
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',
                                filename=filename))
return render_template("index.html")

if __name__ == ('__main__'):
    app.run(debug=True, port=8000)
<!DOCTYPE html>
<html>
<head>
<title>Upload a File!</title>
</head>
<body>
    <form method="post" action="user/Tom/Files/" enctype="multipart/form-data">
      <input type="file" id="customFile" name="file">
      <input type="submit" name="Upload" value="Upload">
    </form>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我删除了操作和值

现在我有这个:

<form method="post" enctype="multipart/form-data">
  <input type="file" id="customFile" name="file">
  <input type="submit" name="Upload">
</form>

但是现在它保留在de index.html上,但没有将其上传到/ user / Tom / Files /吗?