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')
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?
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With