0

使用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>

所以,我需要的是有文本框,当用户按下上传文件时会读取它。

4

1 回答 1

2

似乎我找到了一个解决方案 - 通过上传以一种形式移动文本框。不知道它是否合法,但似乎它正在工作)

替换这个:

   <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>


于 2020-08-27T12:15:49.750 回答