Personal record: web import file to background (ie8 compatible)

———————————————Description ——————————————
The return type is String, because the js of the upload call of ie8 does not support the return
of The foreground is processed into json type
if(d.indexOf("<pre")!=-1){
				        	  d = JSON.parse(jQuery(d).text());
				          }
				          else{
				        	  d = JSON.parse(d);
				          }


——————————————— Non-ie8 ——————————————————
Reference js
<script src="/js/ajaxfileupload.js"></script>

front desk
<input id="updatePhoneExcel" name="updatePhoneExcel" type="file">

js call
    $.ajaxFileUpload({
	                url : '/api/nameList/statechange/getMultiInfoExcel',
	                secureuri : false,
	                fileElementId : 'updatePhoneExcel',
	                dataType : "text",
	                //data:dataJson,
	                success : function(d) {
	                	if(d.indexOf("<pre")!=-1){
				        	  d = JSON.parse(jQuery(d).text());
				          }
				          else{
				        	  d = JSON.parse(d);
				          }
	                	if(d.flag=="true"){
	                		if(d.notExistEmpIds!=""){
	                			toastr.success("Import template data successfully! But "+d.notExistEmpIds+" does not exist in the roster");
	                		}else{
	                			if(d.notScopeEmpIds!=""){
		                			toastr.success("Import template data successfully! But "+d.notScopeEmpIds+" is not within your administrative authority");
		                		}else{
		                			toastr.success("Import template data successfully!");
		                		}
	                		}
	                		self.searchByInfo();
	                	}else{

	                		toastr.error("Failed to import template data!");
	                	}
	                },
	                error : ajaxError,
	    			complete : function() {
	                    $("#showLoading").hideLoading();
	                }
	            });


Backstage
@RequiresAuthentication
@RequiresPermissions("Gbs.Extended.Edit")
@RequestMapping(value = "/getMultiInfoExcel", headers = ("content-type=multipart/*"), method = RequestMethod.POST,produces = "text/plain")
public String getMultiInfoExcel(@RequestParam("updatePhoneExcel") MultipartFile file,HttpServletRequest request, HttpServletResponse response
			,HttpSession session) throws IOException{
		Map<String, Object> returnDataMap = new HashMap<String, Object>();
		session.setAttribute("alllist",null);
		List<UploadTemplateVO> utVOs = new ArrayList<UploadTemplateVO>();
		if (!file.isEmpty()) {
			String[] allowType = { "xls", "xlsx" };
            String filename = file.getOriginalFilename();
            / / Parse the excel file, and then perform database access
            //(1) Determine the file type
            if (filename.indexOf(".") == -1)
    			throw new IOException();
    		String type = filename.substring(filename.indexOf(".") + 1);
    		if (type.length() > 4)
    			throw new IOException();
    		Boolean typeFlag = false;
    		for (int i = 0; i < allowType.length; i++)
    		{
    			if (allowType[i].equalsIgnoreCase(type))
    			{
    				typeFlag = true;
    				break;
    			}
    		}
    		if (!typeFlag)
    			throw new IOException();

    		String realPath = "src\\main\\resources\\template\\uploadTemp";
    		String path = realPath+"\\"+filename;
    		File uploadFile = new File(path);
    		FileUtils.copyInputStreamToFile(file.getInputStream(), uploadFile);

			InputStream inStream = new FileInputStream(uploadFile);

			utVOs = readExcelService.readExcelInfo(inStream);

		} else {
			utVOs = null;
		}
		List<GbsExtendedVO> gbsExtendedVOs = gbsExtendedService.allGbsExtended();
		String notExistEmpIds  = "";
		String notScopeEmpIds  = "";
		if(utVOs!=null){
			for(int i=0;i<utVOs.size();i++){
				boolean existFlag = false;
				boolean scopeFlag = false;
				for(int j=0;j<gbsExtendedVOs.size();j++){

					if(utVOs.get(i).getEmpID().equals(gbsExtendedVOs.get(j).getEmpID())){
						GbsExtendedVO evo = gbsExtendedVOs.get(j);
						String str = findDatascopeComtains(getCurrentUserInfo().getDataScope());
						if(str.contains(evo.getEmpID()) || str.contains("1039")|| str.contains("1001")){
							evo.setTel(utVOs.get(i).getPhone());
							gbsExtendedService.updateGbsExtended2(evo);
							scopeFlag = true;
						}else{
							scopeFlag = false;
						}
						/*DataScopeValidationUtil.validateSingleDatascope(str, "orgUnitID", evo);
						evo.setTel(utVOs.get(i).getPhone());
						gbsExtendedService.updateGbsExtended2(evo);*/
						existFlag = true;
						break;
					}

				}
				if(existFlag == false){
					notExistEmpIds = notExistEmpIds + utVOs.get(i).getEmpID()+",";
				}
				if(scopeFlag == false){
					notScopeEmpIds = notScopeEmpIds + utVOs.get(i).getEmpID()+",";
				}
			}
			if(notExistEmpIds.length()>0){
				notExistEmpIds = notExistEmpIds.substring(0, notExistEmpIds.length()-1);
			}
			if(notScopeEmpIds.length()>0){
				notScopeEmpIds = notScopeEmpIds.substring(0, notScopeEmpIds.length()-1);
			}
			returnDataMap.put("notExistEmpIds", notExistEmpIds);
			returnDataMap.put("notScopeEmpIds", notScopeEmpIds);
			returnDataMap.put("flag", "true");
			return JsonUtil.beanToJson(returnDataMap);
		}
		returnDataMap.put("flag", "false");
		return JsonUtil.beanToJson(returnDataMap);
	}



—————————————————— Split line ie8————————————————————
ie8 calls js and css
<link href="/vendors/uploadify/uploadify.css" rel="stylesheet">
<script src="/vendors/uploadify/jquery.uploadify.min.js"></script>

ie8 calls js
        $("#updatePhoneExcel").uploadify({
		        'swf': '/vendors/uploadify/uploadify.swf',
		        'uploader': '/api/nameList/statechange/getMultiInfoExcel',
		        'fileObjName': 'updatePhoneExcel',
		        'buttonText': 'Please select a file',
		        'fileTypeExts': '*.xls; *.xlsx',
		        'fileSizeLimit': '10MB',
		        'car': false,
		        'multi': false,
		        'onUploadSuccess': function (file, data, response) {
		        	if(d.indexOf("<pre")!=-1){
			        	  d = JSON.parse(jQuery(d).text());
			          }
			          else{
			        	  d = JSON.parse(d);
			          }
		        	if(d.flag=="true"){
                		if(d.notExistEmpIds!=""){
                			toastr.success("Import template data successfully! But "+d.notExistEmpIds+" does not exist in the roster");
                		}else{
                			if(d.notScopeEmpIds!=""){
	                			toastr.success("Import template data successfully! But "+d.notScopeEmpIds+" is not within your administrative authority");
	                		}else{
	                			toastr.success("Import template data successfully!");
	                		}
                		}
                		self.searchByInfo();

                	}else{

                		toastr.error("Failed to import template data!");
                	}

		        },
		        'onUploadError': ajaxError,
		        //Adding this sentence will rewrite the onSelectError method [events that need to be rewritten]
		        'overrideEvents': ['onSelectError', 'onDialogClose'],
		        //returns an error, triggered when a file is selected
		        'onSelectError': function (file, errorCode, errorMsg) {
		          switch (errorCode) {
		            case -110:
		              toastr.error("File[" + file.name + "] size exceeds 300K");
		              break;
		            case -120:
		              toastr.error("File size is abnormal");
		              break;
		            case -130:
		              toastr.error("The file type is incorrect");
		              break;
		          }
	        	},
	     	});


//Call the previous js in the function
        if(getBrowserVersion()=='MSIE8.0'){
			    	if($("#updatePhoneExcel-queue").html()!=""){
			          	ajaxUrl = '/api/nameList/statechange/getMultiInfoExcel';
			            $("#updatePhoneExcel").uploadify('upload');
			            return;
			        }
			    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326519860&siteId=291194637