上传后瓶重定向不起作用 [英] Flask redirect doesn't work after upload
问题描述
@ blah.route( '/ upload',methods = ['GET','POST'])
def upload():$ b $如果request.method =='POST'和'file'在request.files:
file = request.files ['file']
if file:
filename = secure_filename(file.filename)
file.save(os.path.join('./ tmp /上传文件名))
打印'%s文件保存'%文件名
返回重定向(url_for(blah.list_uploads))
返回render_template('blah / upload .html')
编辑:不知道是否有助于说我正在使用DropzoneJS。我认为它默认使用Ajax。也许它有什么用?
Dropzone控制上传过程,所以你必须使用Dropzone重定向jQuery被加载)。
创建一个事件监听器,当队列中的所有文件完成上传时,它将重定向页面:
< form action ={{url_for('upload')}}class =dropzoneid =my-dropzonemethod =POSTenctype =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){$
window.location ={{url_for('upload')}};
});
})
//在队列中的所有文件完成上传时调用。 b $ b< / script>
在视图函数中处理重定向:
@ app.route('/')
def index():
#渲染上传页面
返回render_template('index.html')
@ app.route('/ upload',methods = ['GET','POST'])
def upload() :
if request.method =='POST':
for request.files.getlist('file'):
f.save(os.path.join(app.config (''UPLOADED_PATH'],f.filename))
返回重定向(url_for('重定向到哪里))
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?
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'))
这篇关于上传后瓶重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!