我是Flask的新手。我想创建一个简单的项目,我可以将文件上传到web,调用exe程序来处理文件并输出进程的日志。我试图从http://flask.pocoo.org/docs/0.12/patterns/fileuploads/修改一些文件上传模板,以实现我想要的功能。
我设法浏览并将文件保存到我的目录中,但我无法运行 processing_call 方法。但是,我尝试刷新页面,发现它可以运行exe程序。这意味着 processing_call 有效。但我想要的是调用exe而无需刷新页面。
这是我的代码:
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
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')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
processing_file()
return render_template('uploadfile.html',subprocess_output=processing_file())
答案 0 :(得分:0)
在Python语言中,return
语句之后的代码永远不会起作用(在我所知的所有语言中都是一样的)。因此,如果您需要做某事,请在return
之前执行。如果您不想在执行函数时阻止用户,则需要任务队列(https://www.fullstackpython.com/task-queues.html)以异步方式调用它。