NEXT Vance_19 2023-02-02 14:59 采纳率: 100%
浏览 147
已结题

用flask框架接收前端上传的文本文件后怎么读取该文件

我在做一个可视化的网站,需要用户把数据通过前端传到后端(flask),后端对该文件(.txt,.json)进行读取等一系列操作。
但是server.py中的filename无法被调用,文件名称是个变量,open方法的filepath就不知道怎么办了,还有我是小白,后端数据处理的操作我想放在另外一个python程序中进行,但是我在新文件中导包发现filename无法传过来。

@app.route('/upload', methods=['GET','POST'])
def upload(filename):
    upload_file = request.files['abc']
    if upload_file and allowed_file(upload_file.filename):
        filename = secure_filename(upload_file.filename)
        print(filename)
        upload_file.save(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename))
        return filename
    else:
         return 'failed'

  • 写回答

3条回答 默认 最新

  • Eliot5566 2023-02-02 18:26
    关注

    下面是代码:

    @app.route('/upload', methods=['GET','POST'])
    def upload():
        if request.method == 'POST':
            upload_file = request.files['abc']
            if upload_file and allowed_file(upload_file.filename):
                filename = secure_filename(upload_file.filename)
                file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
                upload_file.save(file_path)
                # 读取文件内容
                with open(file_path, 'r') as f:
                    file_content = f.read()
                    # 处理文件内容
                    # ...
                    return 'success'
            else:
                return 'failed'
        return '''
        <!doctype html>
        <html>
        <head>
            <title>Upload new File</title>
        </head>
        <body>
            <h1>Upload new File</h1>
            <form action="" method=post enctype=multipart/form-data>
                <p><input type=file name=abc>
                   <input type=submit value=Upload>
            </form>
        </body>
        </html>
        '''
    
    
    

    在 upload 函数中,首先判断是否为 POST 请求,如果是,则从 request.files 字典中获取文件对象(此处的 key 为 abc),接着使用 secure_filename 函数处理文件名,生成文件路径,并使用 save 方法保存到服务器端。最后,使用 Python 中的 open 函数读取文件内容,进行处理。

    如果想要在另一个 Python 程序中处理文件内容,可以使用 Flask 提供的 url_for 函数生成文件路径,在新程序中导入 Flask 的应用对象,使用该应用对象获取路径信息。代码示例如下:

    如果想在另一个 Python 程序中处理该文件,可以通过 Flask 提供的 url_for 函数生成文件的 URL,并在新程序中使用 requests 库读取文件内容。代码示例如下:

    server.py
    from flask import Flask, request, url_for
    import os

    app = Flask(name)
    app.config['UPLOAD_FOLDER'] = 'uploads'

    @app.route('/upload', methods=['GET','POST'])
    def upload():
    upload_file = request.files['abc']
    if upload_file and allowed_file(upload_file.filename):
    filename = secure_filename(upload_file.filename)
    file_path = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)
    upload_file.save(file_path)
    return url_for('get_file', filename=filename)
    else:
    return 'failed'

    @app.route('/uploads/')
    def get_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

    new_program.py
    import requests

    url = 'http://localhost:5000/uploads/' + filename
    res = requests.get(url)
    file_content = res.content

    对 file_content 进行处理,比如读取其中的内容,写入到另一个文件等

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 创建了问题 2月2日

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能