[英]Send string from textbox together with file into Flask
使用flask,我正在渲染html页面,我需要在该页面上上传文件并获取文本字符串以与文档一起处理。
第一部分导入和解析文档完美运行。 但是我无法在一个请求中同时导入文件和读取文本框。
是否可以?
我的应用程序.py:
# route and function to handle the upload page
@app.route('/', methods=['GET', 'POST'])
def upload_page():
if request.method == 'POST':
# check if there is a file in the request
if 'file' not in request.files:
return render_template('upload.html', msg='No file selected')
file = request.files['file']
# if no file is selected
if file.filename == '':
return render_template('upload.html', msg='No file selected')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(path)
# text input:
text = request.form['text']
processed_text = text
print(text)
# perform analysis on it
我的上传.html:
<html>
<head>
<title>Upload Docx</title>
</head>
<body>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
<form method=post>
<input name="text">
<input type="submit">
</form>
{% if msg %}
<h1><p style="color:blue";>{{ msg }} </p></h1>
{% endif %}
{% if file_name %}
<p> Imported document: <b> {{file_name}} </b> </p>
{% endif %}
<h1>Result:</h1>
{% if d_type %}
<p> I. Type of document: <b> {{ d_type }} </b> </p>
{% else %}
The analysis result will be displayed here.
{% endif %}
</body>
</html>
所以,我需要的是有文本框,当用户按下上传文件时会读取它。
似乎我找到了一个解决方案 - 通过上传以一种形式移动文本框。 不知道它是否合法,但似乎有效)
替换这个:
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
<form method=post>
<input name="text">
<input type="submit">
</form>
对此:
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input name="text">
<input type=submit value=Upload>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.