上传到Flask应用后,Dropzone不会重定向

时间:2016-07-27 19:08:44

标签: javascript python flask dropzone.js

我正在编写资源,使用Dropzone将文件上传到Flask应用。上传文件后,应用程序应重定向到hello world页面。这没有发生,应用程序停留在上传文件的视图上。我使用的是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 %}

2 个答案:

答案 0 :(得分:0)

我对dropzone不太熟悉,但我会从我的一个使用文件上传的烧瓶应用程序中给出一个示例。我只是使用标准的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

答案 1 :(得分:0)

我在烧瓶应用程序中遇到了类似的问题,我使用以下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()
    });
  }
};

参数&#34;发送&#34;在文件发送之前调用,所以我可以提交我的dropzone表单。有了这个我的烧瓶应用程序中的所有重定向工作正常。

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

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

</form>