将文本框中的字符串与文件一起发送到 Flask

Send string from textbox together with file into Flask

我正在使用 Flask 呈现 html 页面,我需要在该页面上上传文件并获取文本字符串以与文档一起处理。
第一部分导入和解析文档完美运行。但是我无法在一个请求中同时导入文件和读取文本框。
可能吗?

我的app.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

我的upload.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>