———————————————说明———————————————
返回类型为String,因为ie8的上传调用的js不支持json类型的返回
通过前台处理成json类型
class="js" name="code">
if(d.indexOf("<pre")!=-1){
d = JSON.parse(jQuery(d).text());
}
else{
d = JSON.parse(d);
}
————————————————非ie8——————————————-——
引用js
<script src="/js/ajaxfileupload.js"></script>
前台
<input id="updatePhoneExcel" name="updatePhoneExcel" type="file">
js调用
$.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("导入模板数据成功!但"+d.notExistEmpIds+"不存在于名册之中");
}else{
if(d.notScopeEmpIds!=""){
toastr.success("导入模板数据成功!但"+d.notScopeEmpIds+"不在您的管理权限范围内");
}else{
toastr.success("导入模板数据成功!");
}
}
self.searchByInfo();
}else{
toastr.error("导入模板数据失败!");
}
},
error : ajaxError,
complete : function() {
$("#showLoading").hideLoading();
}
});
后台
@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();
//解析excel文件,然后进行数据库存取
//(1)判断文件类型
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);
}
—————————————————分割线 ie8———————————————————
ie8调用js和css
<link href="/vendors/uploadify/uploadify.css" rel="stylesheet">
<script src="/vendors/uploadify/jquery.uploadify.min.js"></script>
ie8调用js
$("#updatePhoneExcel").uploadify({
'swf': '/vendors/uploadify/uploadify.swf',
'uploader': '/api/nameList/statechange/getMultiInfoExcel',
'fileObjName': 'updatePhoneExcel',
'buttonText': '请选择文件',
'fileTypeExts': '*.xls; *.xlsx',
'fileSizeLimit': '10MB',
'auto': 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("导入模板数据成功!但"+d.notExistEmpIds+"不存在于名册之中");
}else{
if(d.notScopeEmpIds!=""){
toastr.success("导入模板数据成功!但"+d.notScopeEmpIds+"不在您的管理权限范围内");
}else{
toastr.success("导入模板数据成功!");
}
}
self.searchByInfo();
}else{
toastr.error("导入模板数据失败!");
}
},
'onUploadError': ajaxError,
//加上此句会重写onSelectError方法【需要重写的事件】
'overrideEvents': ['onSelectError', 'onDialogClose'],
//返回一个错误,选择文件的时候触发
'onSelectError': function (file, errorCode, errorMsg) {
switch (errorCode) {
case -110:
toastr.error("文件 [" + file.name + "] 大小超过300K");
break;
case -120:
toastr.error("文件大小异常");
break;
case -130:
toastr.error("文件类型不正确");
break;
}
},
});
//函数中调用上一个js
if(getBrowserVersion()=='MSIE8.0'){
if($("#updatePhoneExcel-queue").html()!=""){
ajaxUrl = '/api/nameList/statechange/getMultiInfoExcel';
$("#updatePhoneExcel").uploadify('upload');
return;
}
}