Ajax無刷新上傳

html以下:javascript

<form>
	<div id="whiteListDiv" style="display: none;margin:-5px auto 10px auto;" class="form-group clearfix">
		<label><font></font></label>
		<input type="file" id="whiteList" name="whiteList" style="height: 24px; width: 500px;" />
		<input type="button" class="btn-t1" onclick="uploadList('white');" value="上傳" style="width: 120px; margin-left: 20px;">
	</div>
			
				
	<div class="form-group clearfix">
		<label><font></font>是否有黑名單</label>
		<input type="file" id="blackList" name="blackList" style="height: 24px; width: 500px;" />
		<input type="button" class="btn-t1" onclick="uploadList('black');" value="上傳" style="width: 120px; margin-left: 20px;">
	</div>
</form>

注意id和name屬性html

調用的JS方法:java

function uploadList(type){
    	
    	var fileList;
    	if(type == "white"){
    		fileList = $("#whiteList").val();
    	} else {
    		fileList = $("#blackList").val();
    	}
    	
    	if(fileList == null || fileList == ""){
    		alert("請先選擇文件再上傳");
    		return;
    	}
    	
    	var suffix = fileList.substring(fileList.lastIndexOf("."));
    	if(suffix != ".csv" && suffix != ".CSV"){
    		alert("請選擇csv格式文件");
        	return;
    	}
    	
    	var formData = new FormData($("form")[0]);
    	formData.append("idBombBannerInfo",sid);
    	formData.append("type",type);
    	
    	$.ajax({
    		url : '../../accessManagerment/uploadFileList.do',
    		type : 'POST',
    		data : formData, 
        	// 告訴jQuery不要去處理髮送的數據
        	processData : false, 
        	// 告訴jQuery不要去設置Content-Type請求頭
        	contentType : false,
        	beforeSend:function(){
	        	//console.log("正在進行,請稍候");
        			},
        	success : function(data) {
		        		if(data.errorMsg != null && data.errorMsg != ""){
	        				alert(data.errorMsg);
	        			} else {
	        				alert(data.resultMsg);
	        			}
        			}, 
        	error : function(responseStr) { 
        				alert("上傳名單出現異常");
        			} 
    	});
    	
    }

因爲是ajax發起請求,界面不用刷新jquery

後臺獲取參數ajax

@RequestMapping("/accessManagerment/uploadFileList.do")
	public void uploadFileList(HttpServletRequest request, HttpServletResponse response,
								@RequestParam(value="whiteList",required=false) MultipartFile whiteList,
								@RequestParam(value="blackList",required=false) MultipartFile blackList,
								@RequestParam(value="idBombBannerInfo",required=false) String idBombBannerInfo,
								@RequestParam(value="type",required=false) String type
								) throws IOException, ParseException, CmsBusinessException, IllegalStateException, ServletException{
		CmsLogger.audit("弾屏廣告-上傳白名單/黑名單:[type=" + type+"][idBombBannerInfo="+"]");
		Map<String,Object> resultMap  = new HashMap<String,Object>();
		Map<String,Object> paramMap  = new HashMap<String,Object>();
		PrintWriter out = initWriter();
		out = response.getWriter();
		String result = "";
		
		if(!(idBombBannerInfo != null && !idBombBannerInfo.equals("") && type != null && !type.equals(""))){
			result = "{\"errorMsg\": \"上傳參數不能爲空\"}";
			return;
		}
		
		//獲取產品編碼
//		DefaultMultipartHttpServletRequest req = (DefaultMultipartHttpServletRequest) request;
//		String productCode = req.getParameter("productCode");
		long www = whiteList.getSize();
		long bbb = blackList.getSize();
		
		MultipartFile fileList = null;
		if(type != null && type.equals("white")){
			fileList = whiteList;
		} else {
			fileList = blackList;
		}
		try{
			if(fileList == null){
				result = "{\"errorMsg\": \"上傳文件不能爲空\"}";
				return;
			}
			
			//判斷上傳文件的格式(csv)
			String originalName = fileList.getOriginalFilename();
			String suffix = originalName.substring(originalName.lastIndexOf("."));
			if(!suffix.equalsIgnoreCase(".csv")){
				result = "{\"errorMsg\": \"只能上傳csv格式的文件\"}";
				return;
			}
			
			//獲取文件內容
			InputStream is = fileList.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//			String phoneStr = "";
//			String tmp = "";
//			int count = 0;		//表明上傳的手機號數量
//			while((tmp = reader.readLine()) != null){
//				//僅截取每行的前11位--若是長度夠11
//				if(tmp != null && tmp.length() >= 11){
//					//最多傳遞500個手機號
//					if(count < 500){
//						phoneStr += tmp.substring(0,11) + ",";
//						count++;
//					} else {
//						result = "{\"errorMsg\": \"白名單上傳失敗 :白名單數量已超過500\"}";
//						return;
//					}
//				}
//			}
//			
//			if(count == 0){
//				result = "{\"errorMsg\": \"上傳的白名單爲空,請錄入白名單從新上傳!\"}";
//				return;
//			}
//			
//			if(phoneStr.indexOf(",") > -1){
//				phoneStr = phoneStr.substring(0,phoneStr.length() - 1);
//			}
			
			//下面調用接口直接將手機號一併發送過去
//			String URL = "http://IQSZ-L1470:9091/elis_mili_shop_app/do/app/product/factory/batchSaveUserWhiteList";
			String urlSuffix = cachePropertiesUtils.getPropertyValue("mili_shop.http.app.url");
			String URL = urlSuffix + "/do/app/product/factory/batchSaveUserWhiteList";
			
			NameValuePair[] data = {
//					new NameValuePair("whilteList", phoneStr),
//					new NameValuePair("operator", user.getUserUmNo())
					};
			//調用接口
			resultMap.put("resultCode", "00");
			
			if(resultMap != null && resultMap.get("resultCode").equals("00")){
				//mili-shop新增成功
				/*注意添加產品的時候,也要同時添加產品配置信息*/
				result = "{\"resultMsg\": \"上傳成功\"}";
				if(type.equals("white")){
					paramMap.put("isUploadWhitelist", "Y");
				} else {
					paramMap.put("isUploadBlacklist", "Y");
				}
				paramMap.put("sid", idBombBannerInfo);
				
				accessManagermentService.updateBombBannerStatus(paramMap);
				
			} else {
				result = "{\"errorMsg\": \"上傳失敗 " + resultMap.get("resultMsg") + "\"}";
				CmsLogger.audit("弾屏廣告名單上傳失敗");
			}
			
		} catch(Exception e){
			result = "{\"errorMsg\": \"弾屏廣告名單上傳失敗\"}";
			CmsLogger.error("BombBannerManagementController-->uploadFileList:弾屏廣告名單上傳失敗:" + e.getMessage(), e);
		} finally {
			if (null != out) {
				out.print(result);
				out.flush();
				out.close();
			}
		}
	}

jquery引用正常,請求能正常到後臺,應該就能夠直接使用了,上傳代碼是直接可運行的。。。併發

相關文章
相關標籤/搜索