1.框架自带的导入功能按钮,下面二选一即可
<button type="button" id="faupload-student" class="btn btn-info btn-import " data-input-id="c-local" data-mimetype="xls,xlsx" data-preview-id="p-local" data-url="{:url('ajax/upload')}"><i class="fa fa-upload"></i> {:__("导入")}</button>
<a href="javascript:;" data-url="ajax/upload" class="btn btn-info btn-import{:$auth->check('student/import')?'':'hide'}" title="{:__('Import')}" ><i class="fa fa-upload"></i> {:__('导入')}</a>
2.自定义导入按钮
<button type="button" id="faupload-student" class="btn btn-info" data-input-id="c-student" data-mimetype="xls,xlsx" data-multiple="false" data-preview-id="p-student"><i class="fa fa-upload"></i> {:__('导入')}</button>
<input type="file" name="student" id="student" value="" style="display:none;"/>
然后对应的js文件里面需要添加
$('#faupload-student').on('click',function () {
$('#student').click();
});
$('#student').on('change',function () {
if(this.files.length <=0){
return false;
}
var length= this.files.length
var files= this.files
require(['upload'], function(Upload){
console.log(files)
for(var a=0;a<length;a++)
{
Upload.api.send(files[a], function(data, ret){
//
var formData = new FormData();
//
formData.append('file',ret.data.url);
$.ajax({
url:'student/import',
type:'post',
data:formData,
cache: false, //上传文件不需要缓存
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
success:function(data){
console.log(data);
//
// return data.msg;
Toastr.success("成功");
window.parent.location.reload();
return false;
}
})
}, function(data, ret){
Toastr.success("失败");
});
}
});
});
由于时不时的会用到导入功能,这里也记录一下,下次有需要直接复制即可。