赞
踩
在uniapp里面,我们可以先写一个按钮,绑定上传事件
<button @click="test">上传Excel,批量上传</button>
在methods里面,使用uni自带的函数,uni.chooseFile和uni.uploadFile
uni.chooseFile用来选择文件,uni.uploadFile用来上传文件
uni.chooseFile({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
uni.uploadFile({
url: 'url', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{list_id:this.listid}, //携带的参数
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
});
此时向服务器发送的是,带有二进制的文件。
python先用pip安装好 flask、pandas、openpyxl
from flask import Flask,render_template,request,g,session,url_for,send_file import MySQLdb import pandas as pd #MySQL配置 host = 'localhost' user = 'root' password = '123' database = 'pro' app = Flask(__name__) @app.route('/addexcel', methods=['GET', 'POST']) def excel(): db = MySQLdb.connect(host=host, user=user, passwd=password, db=database) cursor = db.cursor() #获取excel文件 file = request.files['file'] #获取参数,可选 list_id = request.values.get("list_id") #pd读取excel df = pd.read_excel(file) #df.index.values,返回的是每行index的数组 for i in df.index.values: #df.iloc可以根据下标和列名取元素,df[下标][列名] que = str(df.iloc[i]["que"]) a = str(df.iloc[i]["a"]) b = str(df.iloc[i]["b"]) c = str(df.iloc[i]["c"]) d = str(df.iloc[i]["d"]) ans = str(df.iloc[i]["ans"]) #上传到数据库即可 cursor.execute("REPLACE into que(que,list_id,a,b,c,d,ans) values(%s,%s,%s,%s,%s,%s,%s)",(que,list_id,a,b,c,d,ans,)) db.commit() return "200"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。