最近因为项目需要研究了下bootstrap fileinput的使用,来记录下这几天的使用心得吧。

前台html页面的代码

    <form role="form" id="importFile" method="post"
enctype="multipart/form-data">
<div class="row">
<div class="col-md-3" >
<input type="radio" name="excelType" class="radio" id="station" value="station"><label for="station">&nbsp;参数1</label>
</div>
<div class="col-md-3 ">
<input type="radio" name="excelType" class="radio" id="line" value="line"><label for="line">&nbsp;参数2</label>
</div>
<div class="col-md-3 ">
<input type="radio" name="excelType" class="radio" id="pipeline" value="pipeline"><label for="pipeline">&nbsp;参数3</label>
</div>
<div class="col-md-3 ">
<input type="radio" name="excelType" class="radio" id="jdj" value="jdj"><label for="jdj">&nbsp;参数4</label>
</div>
</div>
<input id="excelFile" name="excelFile" class="file-loading"
type="file" multiple accept=".xls,.xlsx" data-min-file-count="1"
data-show-preview="true"> <br>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

js进行插件的初始化和一些参数的设置

$("#excelFile").fileinput({
uploadUrl:"rest/import/importExcel",//上传的地址
uploadAsync: true,
language : "zh",//设置语言
showCaption: true,//是否显示标题
showUpload: true, //是否显示上传按钮
browseClass: "btn btn-primary", //按钮样式
allowedFileExtensions: ["xls", "xlsx"], //接收的文件后缀
maxFileCount: 10,//最大上传文件数限制
uploadAsync: true,
previewFileIcon: '<i class="glyphicon glyphicon-file"></i>',
allowedPreviewTypes: null,
previewFileIconSettings: {
'docx': '<i class="glyphicon glyphicon-file"></i>',
'xlsx': '<i class="glyphicon glyphicon-file"></i>',
'pptx': '<i class="glyphicon glyphicon-file"></i>',
'jpg': '<i class="glyphicon glyphicon-picture"></i>',
'pdf': '<i class="glyphicon glyphicon-file"></i>',
'zip': '<i class="glyphicon glyphicon-file"></i>',
},
uploadExtraData: function() {
var extraValue = null;
var radios = document.getElementsByName('excelType');
for(var i=0;i<radios.length;i++){
if(radios[i].checked){
extraValue = radios[i].value;
}
}
return {"excelType": extraValue};
}
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

注意: uploadExtraData函数中只能用原生JS来取值,不能用JQuery来获取值,此参数用来向后台传递附加参数,以方便处理,最简单的写法是:

uploadExtraData: {
"excelType": document.getElementByID('id')
}
  • 1
  • 2
  • 3

文件上传成功后的回调方法


$("#excelFile").on("fileuploaded", function(event, data, previewId, index) {
alert("上传成功!");
$("#excelImport").modal("hide");
//后台处理后返回的经纬度坐标json数组,
var array = data.response;
console.dir(array);
//jquery循环取经纬度坐标
$.each(array,function(index,latAndLon){
var lon = latAndLon.lon;
var lat = latAndLon.lat;
var point = new Point(lon, lat, map.spatialReference);
var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(
SimpleMarkerSymbol.STYLE_CIRCLE).setColor(
new Color([255,255,0,0.5]));
var attr = {"address": "addressName" };
var infoTemplate = new esri.InfoTemplate("标题", "地址 :${address}");
var graphic = new Graphic(point,symbol,attr,infoTemplate);
map.graphics.add(graphic);
}); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

arcgis中点的定义的两种方法:

        var point =  new Point(lon, lat, new SpatialReference({ wkid: 4326 }));
var point = new Point(lon, lat, map.spatialReference);
  • 1
  • 2

后台Java处理,使用common fileupload插件来实现,此处限制只能上传excel 文件

    public JSONArray importExcel(HttpServletRequest request,
HttpServletResponse response) throws Exception { final String allowFileSuffix = "xls,xlsx";
Subject subject = SecurityUtils.getSubject();
String uname = (String) subject.getPrincipal();
String basePath = "D:" + File.separator + uname;
File tmpDir = new File(basePath);// 初始化上传文件的临时存放目录
JSONArray jsonArry = new JSONArray(); if (!tmpDir.exists()) {
tmpDir.mkdirs();
} // 检查输入请求是否为multipart表单数据。
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory dff = new DiskFileItemFactory();// 创建该对象
dff.setRepository(tmpDir);// 指定上传文件的临时目录
dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
// sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);// 指定一次上传多个文件的总尺寸
sfu.setHeaderEncoding("utf-8");
String type = null; List<FileItem> fileItems = new ArrayList<FileItem>();
try {
fileItems = sfu.parseRequest(request);
} catch (FileUploadException e1) {
System.out.println("文件上传发生错误" + e1.getMessage());
}
String fullPath = null;
String fileName = null;
for (FileItem fileItem : fileItems) { // 判断该表单项是否是普通类型
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();// 控件名
String value = fileItem.getString();
if (name.equals("excelType")) {
type = value;
}
} else {
String filePath = fileItem.getName();
if (filePath == null || filePath.trim().length() == 0)
continue;
fileName = filePath.substring(filePath
.lastIndexOf(File.separator) + 1);
String extName = filePath.substring(filePath
.lastIndexOf(".") + 1);
fullPath = basePath + File.separator + fileName;
if (allowFileSuffix.indexOf(extName) != -1) {
try {
fileItem.write(new File(fullPath));
} catch (Exception e) {
e.printStackTrace();
} } else {
throw new FileUploadException("文件格式不正确");
}
}
}
if (type.equals("station")) {
jsonArry = readExcel(fullPath, fileName);
} else if (type.equals("line")) {
System.out.println("===============:line");
} else if (type.equals("pipeline")) {
System.out.println("===============:pipeline");
} else if (type.equals("jdj")) {
System.out.println("===============:jdj");
}
}
return jsonArry;
} // 判断文件类型
public Workbook createWorkBook(InputStream is, String excelFileName)
throws IOException {
if (excelFileName.toLowerCase().endsWith("xls")) {
return new HSSFWorkbook(is);
}
if (excelFileName.toLowerCase().endsWith("xlsx")) {
return new XSSFWorkbook(is);
}
return null;
} public JSONArray readExcel(String basePath, String fileName)
throws FileNotFoundException, IOException {
File file = new File(basePath);
Workbook book = createWorkBook(new FileInputStream(file), fileName);
JSONObject jsonObj = new JSONObject();
JSONArray jsonArry = new JSONArray();
Sheet sheet = book.getSheetAt(0);
for (int i = 3; i < sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
String lon = row.getCell(2).getNumericCellValue() + "";
String lat = row.getCell(3).getNumericCellValue() + "";
jsonObj.put("lat", lat);// 纬度
jsonObj.put("lon", lon);// 经度
jsonArry.add(jsonObj);
}
System.out.println(jsonArry.toString());
return jsonArry;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

基本流程终于走通啦,修改后记录一下跟大家分享,欢迎拍砖~~~ 
不定时更新,查看更新请点击这里

bootstrap fileinput 文件上传的更多相关文章

  1. bootstrap fileinput 文件上传工具

    这是我上传的第二个plugin 首先第一点就是因为这个好看 符合bootstrap的界面风格 第二是可以拖拽(虽然我不常用这个功能 但是这样界面看起来就丰满了很多) 最后不得不吐槽这个的回发事件 我百 ...

  2. bootstrap fileinput+MVC 上传多文件,保存

    新增用户资料,需要用户上传多笔附件,所以就尝试用了fileinput控件,显示效果如图: 首先,先在model中定义数据模型: public partial class create { [Requi ...

  3. SpringBoot+BootStrap多文件上传到本地

    1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...

  4. Bootstrap Fileupload 文件上传

    1.在jsp中引入css与js文件, <link href="${ctx}/plugins/fileup/css/fileinput.css" media="all ...

  5. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  6. 一款基于bootstrap的文件上传插件,现在很多手机webapp都在用

    官网:http://plugins.krajee.com/file-input

  7. 结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程

    1.bootstrap-fileinpu的简单介绍 在前面的随笔,我介绍了Bootstrap-table表格插件的具体项目应用过程,本篇随笔介绍另外一个Bootstrap FieInput插件的使用, ...

  8. JS文件上传神器bootstrap fileinput详解

    Bootstrap FileInput插件功能如此强大,完全没有理由不去使用,但是国内很少能找到本插件完整的使用方法,于是本人去其官网翻译了一下英文说明文档放在这里供英文不好的同学勉强查阅.另外附上一 ...

  9. Bootstrap 文件上传插件 FileInput的使用问题

    : 在使用bootstrap的文件上传插件fileinput http://plugins.krajee.com/file-input的预览功能时,删除预览图片在 bootstrap 模态框中没有用, ...

随机推荐

  1. 用bochs调试自己写的系统引导代码

    1 安装和配置bochs 首先从bochs.sourceforge.net里面把BOCHS给download下来,鉴于Windows的普及,仅仅谈BOCHS在win下的使用方法,其实在其它的OS中方法 ...

  2. 如何在 Kaggle 首战中进入前 10%

    原文:https://dnc1994.com/2016/04/rank-10-percent-in-first-kaggle-competition/ Introduction Kaggle 是目前最 ...

  3. Python 编码风格指南

    原文:http://python.jobbole.com/84618/ 本文超出 PEP8 的范畴以涵盖我认为优秀的 Python 风格.本文虽然坚持己见,却不偏执.不仅仅涉及语法.模块布局等问题,同 ...

  4. IncrediBuild 2.40 过期时间

    IncrediBuild 2.40的License有2个文件CoordLicense.dat和AgentLicense.dat,分别位于Coordinator和Agent安装目录下,这两个文件都是RS ...

  5. Python标准库:内置函数type(object)

    type(object) type(name, bases, dict) 本函数是返回对象的类型对象.仅仅有一个參数object时,直接返回对象的类型对象.假设仅仅是想推断一个对象是否属于某一个类的对 ...

  6. Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】

    原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...

  7. LCD显示——点阵字体

    Bitmap font 点阵字体是把每一个字符都分成16×16或24×24个点,然后用每个点的虚实来表示字符的轮廓. 点阵字体优点是显示速度快,不像矢量字体需要计算:其最大的缺点是不能放大,一旦放大后 ...

  8. stingray后端开发

    stingray可是化后端开发架构 任何web应用的后端职能都是一样的:业务数据的增删改查.后端语言多种多样,但是唯一不变的就是SQL,你用Java也好,PHP,Python也好,最终操作数据库都是一 ...

  9. 移动端自适应布局 rem方案

    1.viewport.js (function(window, document) { // 给hotcss开辟个命名空间,别问我为什么,我要给你准备你会用到的方法,免得用到的时候还要自己写. con ...

  10. unity的 Social API

    孙广东  2015.12.23 Social API Social API 是訪问的Unity 的point 社会功能.如:• 用户配置文件• 好友列表• 成就• 统计 / 排行榜      它提供了 ...