flask 上传文件demo

flask 上传 xlsx 等文件并保存:

from flask import Flask, request, jsonify
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        print(request.files['file'])
        f = request.files['file']
        with open("./tmp.xlsx", 'wb') as fp:
            fp.write(f.read())
        # data_xls = pd.read_excel(f)
        # return data_xls.to_html()
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''@app.route("/export", methods=['GET'])
def export_records():
    return

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

正文完
 
admin
版权声明:本站原创文章,由 admin 2023-11-26发表,共计669字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系tensortimes@gmail.com。
评论(没有评论)
验证码