Flask redirect doesn't work after upload
Asked Answered
P

1

6

I basically want to go to a different page after the upload. What happens here is that the file is uploaded very quickly and saved on the server, but after that the client(my browser) is in the Waiting stage for a minute each time and doesn't even redirect after the wait. If I remove it, I don't get any response back as expected and everything happens within milliseconds.

@blah.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'file' in request.files:
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join('./tmp/uploads', filename))
            print '%s file saved' % filename

            return redirect(url_for("blah.list_uploads"))  
    return render_template('blah/upload.html')

enter image description here

Edit: Not sure if it will help to say that I'm using DropzoneJS. I think by default it uses Ajax. Maybe it has something to with that?

Parceling answered 20/8, 2014 at 19:54 Comment(6)
Your redirect is missing url_for. I'm guessing blah isn't the name of your blueprint so I'm not sure if that was just an omission when you were creating this question.Passing
@Passing It's in my code. A mistaken omission on my part.Parceling
You say that it is not blocking for a minute when you remove the line (and only the line) return redirect(url_for("blah.list_uploads"))? Nothing else? It looks conspicious that it's exactly one minute. Maybe all workers (or more probably you are running only a single worker) are busy with something else, for example long-polling AJAX call.Ramrod
Pardon my ignorance. How would I deal with "long-polling" an AJAX call? I'm using a basic nginx setup on one EC2 node for now. Also it should eventually redirect though, even if the worker is slow, right?Parceling
post your dropzone js codeSissie
Check out my solution!Neoptolemus
N
3

Update: Now you can use Flask-Dropzone, a Flask extension that integrates Dropzone.js with Flask. For this issue, you can set DROPZONE_REDIRECT_VIEW to the view you want to redirect when uploading complete.


Dropzone control the upload process, so you have to use Dropzone to redirect (make sure jQuery was loaded).
Create an event listener, it will redirect page when all files in the queue finish uploading:

<form action="{{ url_for('upload') }}" class="dropzone" id="my-dropzone" method="POST" enctype="multipart/form-data">
</form>

<script src="{{ url_for('static', filename='js/dropzone.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.js') }}"></script>

<script>
Dropzone.autoDiscover = false;

$(function() {
  var myDropzone = new Dropzone("#my-dropzone");
  myDropzone.on("queuecomplete", function(file) {
    // Called when all files in the queue finish uploading.
    window.location = "{{ url_for('upload') }}";
  });
})
</script>

Handle the redirect in view function:

import os
from flask import Flask, render_template, request

app = Flask(__name__)
app.config['UPLOADED_PATH'] = os.getcwd() + '/upload'

@app.route('/')
def index():
    # render upload page
    return render_template('index.html')


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        for f in request.files.getlist('file'):
            f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
    return redirect(url_for('where to redirect'))
Neoptolemus answered 15/2, 2017 at 13:4 Comment(2)
thanks for open sourcing flask-dropzone. Very comprehensive docs!Esperanzaespial
what if you can flash a success message?Anecdotal

© 2022 - 2024 — McMap. All rights reserved.