layui+springboot 上传数据进行处理的一些问题
问题1:统计文件列表的文件数量
files中只有pushFile()等四个方法,我们需要知道一个Object对象中的对象个数,我们可以Object 的Object.keys(obj) 方法
choose: function choose(obj) {
files = this.file = obj.pushFile();
obj.preview(function (index, file, result) {
imgCount = Object.keys(files).length;
});
}
问题2:需要得到我们最新添加的文件,这里我只需要文件列表里是我最新添加的这个文件,我只需要这一个文件,我们使用for循环删除所有的文件,在将最新的文件加进去
choose: function(obj){
var files = this.files;
for (let x in files) {
delete files[x];
}
var that = this;
files = this.files = obj.pushFile();
问题3:layui当文件列表比限制的文件数多一个时才会提示文件数量限制,当我们将文件数量设置为1时,我们现在需要table只显示我们最新选择的文件,但是我们发现table最多可以出现2个记录,我们可以在添加前,进行表单清空,
- 这是我们将文件数量限制为1,我们发现,第一次选择文件会出现
文件.xlsx ,第二次点击选择选择文件会添加红色方框的这条记录,但是上传时就会提醒我们只能上传一条,所以这时我们不需要第一条记录
var tr = that.elemList.find('tr');
tr.remove();
obj.preview(function(index, file, result){......}
问题4:我们发现进度已经达到了100%,但是进度条没有显示,我们查看参数
我们发现index未定义,说明没有传值进来,这时我们对参数进行处理,这里我们因为只有一条数据,所以将表单的第一条记录id命名为progress-demo ,
问题5: 这时前端处理好了,我们将数据传入到后台
我们填写url
var uploadListIns = upload.render({
elem: '#test-upload-testList'
,elemList: $('#test-upload-demoList')
,url: 'http://localhost:8080/file/importExcel'
,accept: 'file'
,exts: 'xls|xlsx'
,multiple: false
,number: 1
,auto: false
,bindAction: '#test-upload-testListAction'
,choose: function(obj){
问题6:后台如何处理
1.这里注意返回的json格式,我们看一下layui的要求 2.对于文件我们如何处理,我们使用transfer方法,将file文件复制到path的路径中,然后进行处理 file.transferTo(new File(path))
package com.knowledge.management.device.controller;
import com.knowledge.management.device.utils.Neo4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/file",produces = "application/json;charset=utf-8")
public class FileController {
@Autowired
Neo4jUtil neo4jUtil;
@RequestMapping("/importExcel")
public Map<String,Object> importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
Map<String,Object> map =new HashMap<>();
System.out.println("进入文件");
if (file.isEmpty()) {
map.put("code","0");
map.put("messag","文件为空");
return map;
}
String path = neo4jUtil.readYaml("FileDiy","savePath")+System.currentTimeMillis()+file.getOriginalFilename();
System.out.println(path);
file.transferTo(new File(path));
neo4jUtil.importData(path);
System.out.println(file.getName());
map.put("code",1);
return map;
}
}
贴上前端代码
<title>功能演示二 - 上传组件</title>
<div class="layui-card layadmin-header">
<div class="layui-breadcrumb" lay-filter="breadcrumb">
<a lay-href="">主页</a>
<a><cite>数据管理</cite></a>
<a><cite>数据导入</cite></a>
</div>
</div>
<div class="layui-fluid">
<div class="layui-row layui-col-space15">
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-header">文件上传</div>
<div class="layui-card-body">
<div class="layui-upload">
<button type="button" class="layui-btn layui-btn-normal" id="test-upload-testList">选择文件</button>
<button type="button" class="layui-btn" id="test-upload-testListAction">开始导入</button>
<div class="layui-upload-list">
<table class="layui-table">
<colgroup>
<col>
<col width="150">
<col width="260">
<col width="150">
</colgroup>
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>上传进度</th>
<th>操作</th>
</tr>
</thead>
<tbody id="test-upload-demoList"></tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
layui.use(['admin', 'upload', 'element'], function(){
var $ = layui.jquery
,upload = layui.upload
,element = layui.element;
var uploadListIns = upload.render({
elem: '#test-upload-testList'
,elemList: $('#test-upload-demoList')
,url: 'http://localhost:8080/file/importExcel'
,accept: 'file'
,exts: 'xls|xlsx'
,multiple: false
,number: 1
,auto: false
,bindAction: '#test-upload-testListAction'
,choose: function(obj){
var files = this.files;
for (let x in files) {
delete files[x];
}
var that = this;
files = this.files = obj.pushFile();
console.log(files);
var tr = that.elemList.find('tr');
tr.remove();
obj.preview(function(index, file, result){
var tr = $(['<tr id="upload-'+ index +'">'
,'<td>'+ file.name +'</td>'
,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
,'<td><div class="layui-progress" lay-filter="progress-demo"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
,'<td>'
,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
,'</td>'
,'</tr>'].join(''));
tr.find('.demo-reload').on('click', function(){
obj.upload(index, file);
});
tr.find('.demo-delete').on('click', function(){
delete files[index];
tr.remove();
uploadListIns.config.elem.next()[0].value = '';
});
that.elemList.append(tr);
element.render('progress');
});
}
,done: function(res){
console.log("res");
if(res.code==1){
console.log("chenggong");
var that = this;
console.log("log");
console.log(that);
var tr = that.elemList.find('tr');
tr.remove();
uploadListIns.config.elem.next()[0].value = '';
}
}
,allDone: function(obj){
console.log(obj)
}
,error: function(index, upload){
var that = this;
var tr = that.elemList.find('tr#upload-'+ index)
,tds = tr.children();
tds.eq(3).find('.demo-reload').removeClass('layui-hide');
}
,progress: function(n, elem, e, index){
element.progress('progress-demo', n + '%');
}
});
});
</script>
|