前端
<input id="fileSelect" name="fileSelect" ref="inputer" type="file"/>
<button class="btn btn-danger" style="margin-bottom: 10px;" @click="fileupdate">导入</button>
JS
fileupdate: function(){
let inputDOM = this.$refs.inputer;
this.file = inputDOM.files[0];
var formdata = new FormData();
formdata.append('fileSelect',inputDOM.files[0]);
axios.post('/test/import', {
params: {
testId:this.testId,
}
})
axios({ url: '/test/import',
method: 'post',
data: formdata,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
},
后端(注意文件编码)
//excel导入
public function import(Request $request){
$filePath = $request->file('fileSelect');
Excel::load($filePath, function($reader) {
$data = $reader->all();
var_dump($data);
},'gbk');
}
$data = Excel::load($path, function ($reader) {}, 'GBK')->get();