我正在制作一个Flask应用程序,当按下按钮时,它会上传视频,进行一些处理并生成图像。我想在我的html中有一个(可能是div
元素,当状态端点返回带有文件名的200时显示图像,但是我不确定如何。我尝试使用ajax获取请求并恢复状态,但是我不确定如何获取它来更新图像。
app.py:
@app.route('/upload', methods=['POST'])
def upload():
# check if the post request has a file
if 'file' not in request.files:
flash('No file in request')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also submit a empty part without filename
if file.filename == '':
flash('No selected file')
if file and allowed_file(file.filename):
video_file = join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename))
file.save(video_file)
job = q.enqueue_call(
generate_barcode,
args=(video_file,)
)
return redirect(url_for('start', filename=convert_filename(video_file), job_id=job.get_id()))
return redirect(url_for('start', error='Sorry, something went wrong while uploading'))
@app.route('/status/<job_id>', methods=['GET'])
def status(job_id):
job = Job.fetch(job_id, connection=conn)
if job.is_queued:
return 'QUEUED', 201
elif job.is_started:
return job.meta.progress, 202
elif job.is_finished:
return job.result, 200 # image filename
elif job.is_failed:
return job.result, 500 # error
else:
return 'ERROR', 500
getImage.js:
function check_progress(job_id) {
function worker() {
$.get('status/' + job_id, function(data) {
if (data.status === 201 || data.status === 202) {
setTimeout(worker, 1000)
} else if (data.status === 200) {
console.log(data); // success
} else {
console.log(data); // error
}
})
}
}