JQGrid之文件上傳

文件/圖片上傳功能,簡單總結以下javascript

1.引入ajaxfileupload.js

注意:該文件須要在引入Jquery以後引入css

下載連接:https://i.cnblogs.com/Files.aspxhtml

2.colModel中文件上傳name設置

 {name:'cover',index:'cover',edittype:"file",editable:true,editoptions: {enctype: "multipart/form-data"},
       formatter:function (value,option,rows) {
           return "<img  style='width:30%;height:10s%;' src='${pageContext.request.contextPath}/image/"+rows.cover+"'/>";
}},

3.前臺編碼

 1 <%@page pageEncoding="UTF-8" contentType="text/html; utf-8" isELIgnored="false" %>
 2 <html>
 3 <link rel="stylesheet" href="statics/boot/css/bootstrap.css" type="text/css">
 4 <link rel="stylesheet" href="statics/jqgrid/css/trirand/ui.jqgrid-bootstrap.css" type="text/css">
 5 <script src="statics/boot/js/jquery-2.2.1.min.js" type="text/javascript"></script>
 6 <script src="statics/boot/js/bootstrap.min.js" type="text/javascript"></script>
 7 <script src="statics/jqgrid/js/trirand/i18n/grid.locale-cn.js" type="text/javascript"></script>
 8 <script src="statics/jqgrid/js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>
 9 <script src="statics/jqgrid/js/ajaxfileupload.js" type="text/javascript"></script>
10 
11 <script>
12     $(function () {
13         $('#tt').jqGrid({
14             url:'${pageContext.request.contextPath}banner/showBanners',
15             datatype:'json',
16             styleUI:'Bootstrap',
17             //cellEdit:true,
18             multiselect:true,
19             colNames:['編號', '名稱', '圖片', '描述', '狀態','建立時間'],
20             collEdit:true,
21             colModel:[
22                 {name:"id",align:'center',hidden:true},
23                 {name:'title',align:'center',editable:true},
24                 {name:'cover',index:'cover',edittype:"file",editable:true,editoptions: {enctype: "multipart/form-data"},
25                     formatter:function (value,option,rows) {
26                         return "<img  style='width:30%;height:10s%;' " +
27                                 "src='${pageContext.request.contextPath}/image/"+rows.cover+"'/>";}},
28                 {name:'description',align:'center',editable:true},
29                 {name:'status',align:'center',editable:true,edittype:"select",
30                     editoptions:{value:"正常:正常;凍結:凍結"}},
31                 {name:'create_date',align:'center',formatter:"date",formatoptions:{scrformat:'Y-m-d H:i:s',newformat:'Y-m-d'}}
32                 //格式化參考:http://www.cnblogs.com/hxling/archive/2010/10/10/1847334.html
33             ],
34             pager:'#pager',
35             autowidth:true,
36             height:'60%',
37             rowNum : 3,
38             rowList : [2,3,4,5],
39             caption : "輪播圖的詳細信息",
40             editurl:'${pageContext.request.contextPath}/banner/oper',//設置編輯表單提交路徑
41             viewrecords : true,
42             //recreateForm: true確保每添加或編輯表單是從新建立。
43         }).navGrid('#pager',{edit : true,add : true,del : true,search:false},
44 
45             {
46                 jqModal:true,closeAfterAdd: true,recreateForm:true,onInitializeForm : function(formid){
47                     $(formid).attr('method','POST');
48                     $(formid).attr('action','');
49                     $(formid).attr('enctype','multipart/form-data');
50                 },
51                 afterSubmit:function (response) {
52                     var status = response.responseJSON.status;
53                     var id = response.responseJSON.message;
54                     alert("確認修改")
55                     if(status){
56                         $.ajaxFileUpload({
57                             url:"${pageContext.request.contextPath}/banner/upload",
58                             fileElementId:"cover",
59                             data:{id:id},
60                             type:"post",
61                             success:function () {
62                                 $("#tt").trigger("reloadGrid")
63                             }
64                         });
65                     }
66                 }
67             },
68 
69             {
70                 jqModal:true,closeAfterEdit: true,recreateForm:true,onInitializeForm : function(formid){
71                     $(formid).attr('method','POST');
72                     $(formid).attr('action','');
73                     $(formid).attr('enctype','multipart/form-data');
74                 },
75                 afterSubmit:function (response) {
76                     var status = response.responseJSON.status;
77                     var id = response.responseJSON.message;
78                     alert("確認添加")
79                     if(status){
80                         $.ajaxFileUpload({
81                             url:"${pageContext.request.contextPath}/banner/upload",
82                             fileElementId:"cover",
83                             data:{id:id},
84                             type:"post",
85                             success:function () {
86                                 $("#tt").trigger("reloadGrid")
87                             }
88                         });
89                     }
90                 }
91             }
92         );
93     })
94 </script>
95 <body>
96 <table id="tt"></table>
97 <div id="pager" style="height: 30px"></div>
98 </body>
99 </html>
前臺代碼

 

4.後臺編碼

 1 public void upload(String id, MultipartFile cover) throws IOException {
 2 //須要在submit以後進行一次圖片路徑的修改
 3         Banner banner = new Banner();
 4         log.info("上傳圖片的原始名字"+cover.getOriginalFilename());
 5         String realPath =httpSession.getServletContext().getRealPath("image");
 6         cover.transferTo(new File(realPath,cover.getOriginalFilename()));
 7         banner.setId(id);
 8         banner.setCover(cover.getOriginalFilename());
 9         bannerDAO.updateByPrimaryKeySelective(banner);
10     }
後臺代碼
相關文章
相關標籤/搜索