烧瓶重定向后,我的数据无效

时间:2019-04-23 14:46:19

标签: python jquery flask dropzone.js

我想在Flask中使用Dropzone.js。上传文件后,我要保存文件并在另一页中显示上传的文件名(重定向后)。但是在浏览器中,我收到的文件名等于“ sample_value”。该如何解决?

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)

模板

<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>

我如何理解在处理请求之前执行重定向?

1 个答案:

答案 0 :(得分:0)

我假设您未在Dropzone中指定方法,则默认情况下它使用 GET 方法。检查Dropzone documentation并将方法指定为 POST

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

>>> print(n) # n has never been replaced
sample_value