Dropzone't上传到Flask应用程序后重定向

Dropzone doesn't redirect after upload to Flask app

本文关键字:Flask 应用程序 重定向 Dropzone      更新时间:2023-09-26

我正在编写一个资源,使用Dropzone将文件上传到Flask应用程序。文件上传后,应用程序应重定向到hello世界页面。这种情况没有发生,应用程序被困在上传文件的视图上。我使用的是来自master的jQuery 3.1.0和Dropzone。

from flask import Flask, request, flash, redirect, url_for render_template)
from validator import Validator
ALLOWED_EXTENSIONS = set(['csv', 'xlsx', 'xls'])
def allowed_file(filename):
    return (filename != '') and ('.' in filename) and '
           (filename.split('.')[-1] in ALLOWED_EXTENSIONS)
def create_app():
    app = Flask(__name__)
    app.secret_key = 'super secret key'
    return app
app = create_app()
@app.route('/')
def index():
    return render_template('index.html')
@app.route('/world')
def hello_world():
    return render_template('hello_world.html')
@app.route('/upload', methods=['POST'])
def upload():
    # check that a file with valid name was uploaded
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    if not allowed_file(file.filename):
        flash('No selected file')
        return redirect(request.url)
    # import ipdb; ipdb.set_trace()
    validator = Validator(file)
    validated = validator.validate()
    if validated:
        flash('Success')
    else:
        flash('Invalid file')
    return redirect(url_for('hello_world'))
if __name__ == '__main__':
    app.run(debug=True)
{% extends "base.html" %}
{% block head %}
    <link href="/static/css/dropzone.css" rel="stylesheet">
    <script src="/static/js/dropzone.js"></script>
{% endblock %}
{% block body %}
    <main>
        <section>
            <div id="dropzone">
                <form action="upload" method="post" class="dropzone dz-clickable" id="demo-upload" multiple>
                    <div class="dz-message">
                        Drop files here or click to upload.
                    </div>
                </form>
            </div>
        </section>
    </main>
{% endblock %}

我在烧瓶应用程序中遇到了类似的问题,我用以下jQuery函数解决了它:

Dropzone.options.myDropzone = {
autoProcessQueue: false,
init: function() {
    var submitButton = document.querySelector("#upload-button");
    myDropzone = this;
    submitButton.addEventListener("click", function() {
        myDropzone.processQueue();
    });
    this.on("sending", function() {
        $("#myDropzone").submit()
    });
  }
};

参数"sending"是在文件发送之前调用的,这样我就可以提交我的dropzone表单。有了这个,我的flask应用程序中的所有重定向都可以正常工作。

为了清晰起见,我的一段html代码:

<form action="/" method="POST" class="dropzone" id="myDropzone" enctype="multipart/form-data">
</form>

我对dropzone不太熟悉,但我将给您举一个使用文件上传的flask应用程序的例子。我只是使用标准的HTML上传表单。希望从这里你应该能够了解发生了什么。

注意,我没有使用模板上传文件。

def index():
    return """<center><body bgcolor="#FACC2E">
    <font face="verdana" color="black">
    <title>TDX Report</title>
    <form action="/upload" method=post enctype=multipart/form-data>
    <p><input type=file name=file>
    <input type=submit value=Upload>
    </form></center></body>"""
# here is my function that deals with the file that was just uploaded
@app.route('/upload', methods = ['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        f.save(f.filename)
        # process is the function that i'm sending the file to, which in this case is a .xlsx file
        return process(f.filename)

这一行是我设置文件上传后的路径的地方:

<form action="/upload" method=post enctype=multipart/form-data>

你的问题可能是这句话:<form action="upload" method="post" class="dropzone dz-clickable" id="demo-upload" multiple>缺少upload之前的/