需求
之前导入表格时总会把表格存到服务器指定路径,再从指定路径读取,会造成一定性能损失,也会产生很多临时文件(不安全)。所以打算使用StringIO直接在内存中操作。
错误写法
想当然的错误写法
@bp.rounte('/import', methods=['POST'])
def import():
upload_excel = request.files['upload_excel'] # upload_excel 为上传表单name
if upload_excel:
f = StringIO.StringIO(upload_excel)
# 或者 f = StringIO.StringIO(upload_excel.content)
# 或者 f = StringIO.StringIO(upload_excel.read())
workbook = xlrd.open_workbook(file_contents=f.getvalue())
# 数据处理
return
正确写法
@bp.rounte('/import', methods=['POST'])
def import():
upload_excel = request.files['upload_excel'] # upload_excel 为上传表单name
if upload_excel:
workbook = xlrd.open_workbook(file_contents=upload_excel.read())
# 数据处理
return