1

I want to use Dropzone.js with Flask. After uploading file i want save file and show uploaded file name in another page (after redirect). But in browser i receive file name equals "sample_value". How to fix this?

Python

import os
from flask import Flask, render_template, request,redirect, url_for

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():
    n='sample_value'
    if request.method == 'POST':
        for f in request.files.getlist('file'):
            n=f.filename
            f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
            print(f.filename)
            # There i get real file name
            n=f.filename
    return redirect(url_for('found', file_name=n), code=307)

@app.route('/found',methods=['GET', 'POST'])
def found():
        #There my file name is "sample_value"
    print('File name after redirect ', request.args.get('file_name'))
    return request.args.get('file_name')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port =5000, debug=True, threaded=True)

Template

<html>
<body>
<script src="{{ url_for('static', filename='js/dropzone.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.js') }}"></script>
<form action="{{ url_for('upload') }}" class="dropzone" id="my-dropzone" method="POST" enctype="multipart/form-data">
</form>
<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>
</body>
</html>

How i understand redirection executes before processing request?

1 Answers1

0

I assume that you do not specify the method in your Dropzone, then it uses GET method by default. Check the Dropzone documentation and specify the method as POST.

n="sample_value"
if request.method == 'POST': # This is always false.
    ...

>>> print(n) # n has never been replaced
sample_value
Victor
  • 602
  • 4
  • 12
  • if request.method == 'POST': is true and when i print(f.filename) i receive real file name. But when i redirect(url_for('found', file_name=n), code=307) - file name is sample_value – Alexander Vedmed' Apr 24 '19 at 08:23
  • It is a problem of HTTP code 302 for redirection interpreted by web browsers. Many web browsers just redirect with a GET method. A solution could be to specify the code of request : flask.redirect(flask.url_for('operation'), code=307) [See explications and solutions here](https://stackoverflow.com/a/15480983/9515831) – Victor Apr 24 '19 at 17:56
  • I pointed it out in the example earlier. return redirect(url_for('found', file_name=n), code=307) – Alexander Vedmed' Apr 25 '19 at 12:40
  • I have tested your code, 127.0.0.1 - POST /upload HTTP/1.1 307 - File name after redirect test.py 127.0.0.1 - POST /found?file_name=test.py HTTP/1.1 200 - And I received the correct file name after redirection. – Victor Apr 25 '19 at 14:56