vue项目多列导入

用axios.post传一个数组参数使用:JSON.stringify(this.params)

1 
2 class="upimg">"img">"file" class="file" name="file" @change="getFile($event)"> 3 4
 1 import XLSX from 'xlsx';
 2     export default{
 3         name: 'cqadd',
 4         data(){
 5             return {
 6                 file: '',
 7                 img: '../src/assets/images/Upload-picture.png',
 8                 jsondata: []
 9             }
10         },
11         methods: {
12             //导入excel获取到内容
13             getFile(event){
14                 //wb 存储读取完成的数据
15                 //jsondata 存储获取excel json数据
16                 var wb,jsondata,that=this;
17                 var f = event.target.files[0];
18                 var reader = new FileReader();
19                 reader.onload = function(e){
20                     var data = e.target.result;
21                     wb = XLSX.read(data, {type: "binary"});
22                     that.jsondata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
23                 }
24                 reader.readAsBinaryString(f);
25             },
26             submitForm(event){
27                 event.preventDefault();
28 
29                 let params = new URLSearchParams();
30 
31                 params.append('jsondata', JSON.stringify(this.jsondata));
32 
33                 this.axios.post('url', params).then((ret)=>{
34                     if(ret.status === 200){
35                         this.$router.push(ret.data.requireUrl);
36                     }
37                 })
38             }
39         },
40         mounted(){
41             
42         }
43     }

后端接收

$jsondata = $_POST["jsondata"];

$arr = json_decode($jsondata);//需要json_decode()下再使用

 1 php 
 2     require '../config/config.php';
 3     $conn = Connect();
 4 
 5     $patterns = array('\'', '"');
 6     $replacements = array('&apos', '"');
 7 
 8     $jsondata = $_POST["jsondata"];
 9     $arr = json_decode($jsondata);
10 
11     $sql = "INSERT INTO cquestion (question, answer, optionone, optiontwo, optionthree, tips) VALUES (?,?,?,?,?,?)";
12 
13     $stmt = mysqli_stmt_init($conn);
14 
15     if(mysqli_stmt_prepare($stmt, $sql)){
16         mysqli_stmt_bind_param($stmt, 'ssssss', $question, $answer, $optionone, $optiontwo, $optionthree, $tips);
17         // 注 此处's'如只给一个字段插入就只写一个s,插入几个字段的数据就写几个
18 
19         for($i=0;$i){
20             $question = str_replace($patterns, $replacements, $arr[$i]->question);
21             $answer = str_replace($patterns, $replacements, $arr[$i]->answer);
22             $optionone = str_replace($patterns, $replacements, $arr[$i]->optionone);
23             $optiontwo = str_replace($patterns, $replacements, $arr[$i]->optiontwo);
24             $optionthree = str_replace($patterns, $replacements, $arr[$i]->optionthree);
25             if(empty($arr[$i]->tips)){
26                 $tips = "";
27             }else{
28                 $tips = str_replace($patterns, $replacements, $arr[$i]->tips);
29             }
30             mysqli_stmt_execute($stmt);
31         }
32 
33         $arr=[
34                 "status"=>200,
35                 "message"=>"add data success",
36                 "requireUrl"=>"/cqlist"
37             ];
38         echo json_encode($arr);
39     }else{
40         echo "创建数据表错误:" . $conn->error;
41     }
42     
43 
44     $conn->close();
45  ?>

 

转载于:https://www.cnblogs.com/studycollect/p/11315655.html

你可能感兴趣的:(vue项目多列导入)