1. 程式人生 > >springMvc Ajax 檔案上傳

springMvc Ajax 檔案上傳

/**
     * 儲存
     * @param json
     * @return
     */
    @RequestMapping(value = "/saveProdBrand",method=RequestMethod.POST)
    @ResponseBody
    public Object saveProdBrand(@RequestParam("uploadLogoFile") CommonsMultipartFile logoFile,
   
@RequestParam("uploadBannerFile") CommonsMultipartFile bannerFile,
    @RequestParam("prodBrandName") String prodBrandName,
    @RequestParam("prodBrandDesc") String prodBrandDesc,HttpServletRequest request) {
    String logoFilename = logoFile.getFileItem().getName().trim();
   
String bannerFilename = bannerFile.getFileItem().getName().trim();
try {
String filePath = request.getSession().getServletContext().getRealPath("upload");
File f = new File(filePath);
if (!f.exists()) {
f.mkdir();
}
//1--logo
String logoPath = filePath + "\\" + logoFilename;
File targetLogoFile = new File(logoPath);
logoFile.getFileItem().write(targetLogoFile);
//2--banner
String bannerPath = filePath + "\\" + bannerFilename;
File targetBannerFile = new File(bannerPath);
bannerFile.getFileItem().write(targetBannerFile);
//3 save
ProdBrand prodBrand = new ProdBrand();
prodBrand.setProdBrandDesc(prodBrandDesc);
prodBrand.setProdBrandName(prodBrandName);
prodBrand.setProdBrandLogoUrl(logoPath);
prodBrand.setProdBrandStatus("1");
prodBrand.setProdBrandBannerUrl(bannerPath);
    prodBrandService.insertSelective(prodBrand);
} catch (Exception e) {
e.printStackTrace();
}
ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS,ResultUtil.SUCCESS_INFO);
return resultUtil;

}




<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap-select.min.css">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/reset.css">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/prodBrand/prodBrandList.css">
    <jsp:include page="/WEB-INF/view/common/include.jsp"></jsp:include>
    </head>
    <body>
    <h1><!-- 推薦管理 --><span> 新增品牌</span></h1>
  <form  id="frmUpload" action="${pageContext.request.contextPath}/prodBrand/saveProdBrand.do"
     method="post" enctype="multipart/form-data">
    <div class="brandcont">
           <div>
               <span>品牌名稱</span>
               <input type="text" name="prodBrandName" id="prodBrandName" />
           </div>
           <div>
               <span>品牌logo</span>
               <input type="file" name="uploadLogoFile" id="uploadLogoFile" />
               <i>建議圖片尺寸120x120</i>
           </div>
           <div>
               <span>品牌banner</span>
               <input type="file" name="uploadBannerFile" id="uploadBannerFile" />
               <i>建議圖片尺寸1900x160</i>
           </div>
           <div>
               <span>品牌簡介</span>
               <textarea name="prodBrandDesc" id="prodBrandDesc"></textarea>
           </div>
           <div class="btnbf">
               <button class="btn btn-info" onclick="saveFile()">儲存</button>
               <a href="${pageContext.request.contextPath}/prodBrand/prodBrandList.do">
               <button class="btn btn-info">返回</button>
               </a>
           </div>
    </div>
     </form>
          
         
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-form.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap-select.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/prodBrand/prodBrandEdit.js"></script>
    
      


     
    </body>
</html>




/**
 * 上傳檔案
 */
function saveFile(){
var uploadBannerFile=$("#uploadBannerFile").val();
var uploadLogoFile=$("#uploadLogoFile").val();
if(uploadBannerFile!="" && uploadLogoFile!=null){


        var options  = {    
            url:ctx+'/prodBrand/saveProdBrand.do',    
            type:'post', 
            success:function(data)    
            {    
            alert(data.resultInfo);
            }    
        };    
        $("#frmUpload").ajaxSubmit(options);  
}else{
alert("請選擇檔案!");
}
}