关于bootstrap-fileinput

最近搞了一个很简单的项目,里面需要文件上传。当然文件上传也是很简单的,不过做出成品之后发现,卧槽,火狐和谷歌两个浏览器显示的内容不一致。
如下图,左边的是谷歌显示,右边是火狐显示。


其实,作为一个后台开发人员,功能实现了就OK了。不过,咱们还是得精益求精不是。向我理工大的崔老师致敬。
百度了一下,发现bootstrap fileinput这个组件不错。


bootstrap-fileinput源码: https://github.com/kartik-v/bootstrap-fileinput
bootstrap-fileinput在线API: http://plugins.krajee.com/file-input
bootstrap-fileinput Demo展示: http://plugins.krajee.com/file-basic-usage-demo


OK下载来看一下,文件夹内容如下,大家看看sample里面的就OK。


这是我改动的一个例子,大家看一下:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10. <head>  
  11. <base href="<%=basePath%>">  
  12.   
  13.   
  14. <title>My JSP 'index.jsp' starting page</title>  
  15. <meta http-equiv="pragma" content="no-cache">  
  16. <meta http-equiv="cache-control" content="no-cache">  
  17. <meta http-equiv="expires" content="0">  
  18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19. <meta http-equiv="description" content="This is my page">  
  20. <link href="css/bootstrap.min.css" rel="stylesheet">  
  21. <link href="css/fileinput.css" media="all" rel="stylesheet" type="text/css" />  
  22.   
  23.   
  24. <script src="js/jquery-1.11.2.js"></script>  
  25. <script src="js/fileinput.min.js" type="text/javascript"></script>  
  26. <script src="js/fileinput_locale_zh.js" type="text/javascript"></script>  
  27. <script src="js/bootstrap.min.js" type="text/javascript"></script>  
  28.   
  29.   
  30. </head>  
  31.   
  32.   
  33. <body>  
  34.     <div class="container kv-main" style=" width: 830px;height: 400px;margin-top: 200px;">  
  35.   
  36.   
  37.         <form enctype="multipart/form-data">  
  38.             <input id="file-1" class="file" type="file" multiple  
  39.                 data-min-file-count="1"> <br>  
  40.         </form>  
  41.   
  42.   
  43.         <hr>  
  44.   
  45.   
  46.         <hr>  
  47.         <br>  
  48.     </div>  
  49. </body>  
  50. </html>  
  51. <script>  
  52.       
  53.     $("#file-1").fileinput({   
  54.         language: 'zh',  
  55.         uploadUrl: 'upload', // you must set a valid URL here else you will get an error  
  56.         allowedFileExtensions : ['xls','jpg', 'png','gif'],  
  57.         maxFileCount: 3,   //同时最多上传3个文件  
  58.         //allowedFileTypes: ['image', 'video', 'flash'],  这是允许的文件类型 跟上面的后缀名还不是一回事  
  59.         //这是文件名替换  
  60.     slugCallback: function(filename) {  
  61.             return filename.replace('(', '_').replace(']', '_');  
  62.         }  
  63.     });   
  64.           //这是提交完成后的回调函数    
  65.      $("#file-1").on("fileuploaded", function (event, data, previewId, index) {  
  66.          top.location.href="processor.jsp";  
  67.      });  
  68. </script>  




我们再看看后台的处理逻辑
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.             throws ServletException, IOException {  
  3.         File file1 = null;  
  4.         response.setCharacterEncoding("UTF-8");  
  5.         request.setCharacterEncoding("UTF-8");  
  6.         response.setContentType("text/html");  
  7.           
  8.          DiskFileItemFactory factory = new DiskFileItemFactory();  
  9.                   ServletFileUpload upload = new ServletFileUpload(factory);  
  10.         try {  
  11.             List<FileItem> list = upload.parseRequest(request); //解析request请求  
  12.               
  13.             for (FileItem fileItem : list) {  
  14.                 System.out.println(fileItem.getFieldName());  
  15.                 if (fileItem.getFieldName().equals("file_data")) {  
  16.                     file1 = new File(getServletContext().getRealPath("attachment"), "myfile.xls");  
  17.                     file1.getParentFile().mkdirs();  
  18.                     file1.createNewFile();  
  19.                                         System.out.println(fileItem.getName()+" psd");  
  20.                     InputStream ins = fileItem.getInputStream();  
  21.                     OutputStream ous = new FileOutputStream(file1);  
  22.                     try {  
  23.                         byte[] buffer = new byte[1024];  
  24.                         int len = 0;  
  25.                         while ((len = ins.read(buffer)) > -1)  
  26.                             ous.write(buffer, 0, len);  
  27.                     } finally {  
  28.                         ous.close();  
  29.                         ins.close();  
  30.                     }  
  31.                 }  
  32.   
  33.   
  34.             }  
  35.         } catch (FileUploadException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.           
  39.   
  40.   
  41.          JSONObject jsonObject = new JSONObject();    
  42.          jsonObject.put("result""ok");  
  43.          response.getWriter().write(jsonObject.toString());  
  44.     }  
处理完成后,必须返回一个json数据,否则会报如下的错误






大家还有不清楚的,在下面回复吧。


参考资料
JS组件系列——Bootstrap文件上传组件:bootstrap fileinput

基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用

http://stackoverflow.com/questions/30939225/bootstrap-file-input-jquery-plugin-designed-by-krajee-syntaxerror-unexpected-e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值