前端代码
upload.render({
elem:'#load',
url:'/data/uploadStuInfo',
accept:'file',
field: "stuInfo", //后端利用request.files['stuInfo']来获取上传的文件
exts:'xls|xlsx|csv', //允许上传的类型
size:'5*1024',
before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
index = layer.load(1); //上传loading //上传logo
},
done:function (res) {
layer.close(index)
layer.msg('上传成功')
},
error:function () {
console.log('上传失败')
}
})
后端代码
将前端传过来的二进制数据利用io.BytesIO处理,之后就可以直接使用pandas.read_excel读取前端上传的excel了。
#上传文件
def uploadStuInfo():
print(request.files)
#利用前端设置的field属性,来获取上传的文件
file = request.files['stuInfo']
#获取文件名
print(file.filename)
#获取文件二进制数据
fileContent = file.read()
#将bytes转化为流
res = io.BytesIO(fileContent)
#使用pandas.read_excel读取
data =pd.read_excel(res)
print(data)
return jsonify({"code": 0})
print结果
xlrd更新到了2.0.1版本,只支持.xls文件。所以pandas.read_excel(‘xxx.xlsx’)会报错。
解决方法:
pip uninstall xlrd
pip install xlrd==1.2.0