fileuploadfield 上传原文件名及原文件类型的取法
2010-06-04 14:29
447 查看
在ext中fileuploadfield 组件提供了文件上传的功能,可是问题在于如果我们在后台用File取得上传的文件,ext会只给你一个缓存文件,也就是说原文件的信息不见了!那么如何取得原文件的信息呢?请看下面代码:
在JS中有这么一段代码:
{
xtype:"fieldset",
labelWidth:1,
title:"导入XML",
items:[{
height:5,
border:false
},{
xtype:"fileuploadfield",
name:"serviceXml",
buttonText: '上传标准XML文件',
buttonOnly: true,
buttonCfg: {
height:15,
width:150
},
listeners: {
'fileselected': {
fn:this.xmlUpload,
scope:this
}
}
}]
}
此处是生成一个fileuploadfield组件,名字为serviceXml,是想上传xml文件
后台:
private File serviceXml; //上传文件
private String serviceXmlFileName; //上传原文件名
private String serviceXmlContentType;//上传原文件类型
//上传xml
public String uploadXml() {
boolean flag = false;
jsonObject = new JSONObject();
try{
if (serviceXml != null) {
//取得原文件类型
String fileType = serviceXmlContentType.substring(serviceXmlContentType.indexOf("/") + 1);
if(fileType.indexOf("xml")>0){
SAXReader read = new SAXReader();
Document document = read.read(serviceXml);
Element root = document.getRootElement();
List<Element> nodes = root.elements();
for(Element node : nodes){
String nodeName = node.getName();
String nodeValue = node.getText();
if("url".equals(nodeName)) jsonObject.put("url", nodeValue);
if("serviceType".equals(nodeName)) jsonObject.put("serviceType", nodeValue);
if("title".equals(nodeName)) jsonObject.put("title", nodeValue);
if("serviceTypeVersion".equals(nodeName)) jsonObject.put("serviceTypeVersion", nodeValue);
if("purpose".equals(nodeName)) jsonObject.put("purpose", nodeValue);
if("dateTime".equals(nodeName)) jsonObject.put("dateTime", nodeValue);
if("keywrds".equals(nodeName)) jsonObject.put("keywrds", nodeValue);
if("mabstract".equals(nodeName)) jsonObject.put("mabstract", nodeValue);
if("individualName".equals(nodeName)) jsonObject.put("individualName", nodeValue);
if("organisationName".equals(nodeName)) jsonObject.put("organisationName", nodeValue);
if("deliveryPoint".equals(nodeName)) jsonObject.put("deliveryPoint", nodeValue);
if("administrativeArea".equals(nodeName)) jsonObject.put("administrativeArea", nodeValue);
if("city".equals(nodeName)) jsonObject.put("city", nodeValue);
if("voice".equals(nodeName)) jsonObject.put("voice", nodeValue);
if("telphone".equals(nodeName)) jsonObject.put("telphone", nodeValue);
if("email".equals(nodeName)) jsonObject.put("email", nodeValue);
}
flag = true;
}else{
this.info = "请上传XML文件";
}
} else {
this.info = "XML上传失败,请检查网络状态!";
}
} catch(Exception e) {
e.printStackTrace();
log4jExecuteIndex.executeFunctionLog(super.getUser().getLoginame(), getRequest(), Log4jExecuteImpl.operateStateFail);
this.info = "XML上传失败,请检查网络状态!";
}
log4jExecuteIndex.executeFunctionLog(super.getUser().getLoginame(), getRequest(), Log4jExecuteImpl.operateStateSuccess);
jsonObject.put("success", true);
jsonObject.put("flag", flag);
jsonObject.put("msg", this.info);
return "jsonObject";
}
相信看了上段代码,大家都会明白,在ext的fileuploadfield组件中,定义了一个对象后,它会自动生成两个变量来装填这个原对象的属性,例如将组件名命名为OBJ那么它就会生成OBJFileName和OBJContentType这两个变量,而这两个变量中装的就是原文件所对应的名字和类型
注意:这三个变量一定要有getter和setter方法
在JS中有这么一段代码:
{
xtype:"fieldset",
labelWidth:1,
title:"导入XML",
items:[{
height:5,
border:false
},{
xtype:"fileuploadfield",
name:"serviceXml",
buttonText: '上传标准XML文件',
buttonOnly: true,
buttonCfg: {
height:15,
width:150
},
listeners: {
'fileselected': {
fn:this.xmlUpload,
scope:this
}
}
}]
}
此处是生成一个fileuploadfield组件,名字为serviceXml,是想上传xml文件
后台:
private File serviceXml; //上传文件
private String serviceXmlFileName; //上传原文件名
private String serviceXmlContentType;//上传原文件类型
//上传xml
public String uploadXml() {
boolean flag = false;
jsonObject = new JSONObject();
try{
if (serviceXml != null) {
//取得原文件类型
String fileType = serviceXmlContentType.substring(serviceXmlContentType.indexOf("/") + 1);
if(fileType.indexOf("xml")>0){
SAXReader read = new SAXReader();
Document document = read.read(serviceXml);
Element root = document.getRootElement();
List<Element> nodes = root.elements();
for(Element node : nodes){
String nodeName = node.getName();
String nodeValue = node.getText();
if("url".equals(nodeName)) jsonObject.put("url", nodeValue);
if("serviceType".equals(nodeName)) jsonObject.put("serviceType", nodeValue);
if("title".equals(nodeName)) jsonObject.put("title", nodeValue);
if("serviceTypeVersion".equals(nodeName)) jsonObject.put("serviceTypeVersion", nodeValue);
if("purpose".equals(nodeName)) jsonObject.put("purpose", nodeValue);
if("dateTime".equals(nodeName)) jsonObject.put("dateTime", nodeValue);
if("keywrds".equals(nodeName)) jsonObject.put("keywrds", nodeValue);
if("mabstract".equals(nodeName)) jsonObject.put("mabstract", nodeValue);
if("individualName".equals(nodeName)) jsonObject.put("individualName", nodeValue);
if("organisationName".equals(nodeName)) jsonObject.put("organisationName", nodeValue);
if("deliveryPoint".equals(nodeName)) jsonObject.put("deliveryPoint", nodeValue);
if("administrativeArea".equals(nodeName)) jsonObject.put("administrativeArea", nodeValue);
if("city".equals(nodeName)) jsonObject.put("city", nodeValue);
if("voice".equals(nodeName)) jsonObject.put("voice", nodeValue);
if("telphone".equals(nodeName)) jsonObject.put("telphone", nodeValue);
if("email".equals(nodeName)) jsonObject.put("email", nodeValue);
}
flag = true;
}else{
this.info = "请上传XML文件";
}
} else {
this.info = "XML上传失败,请检查网络状态!";
}
} catch(Exception e) {
e.printStackTrace();
log4jExecuteIndex.executeFunctionLog(super.getUser().getLoginame(), getRequest(), Log4jExecuteImpl.operateStateFail);
this.info = "XML上传失败,请检查网络状态!";
}
log4jExecuteIndex.executeFunctionLog(super.getUser().getLoginame(), getRequest(), Log4jExecuteImpl.operateStateSuccess);
jsonObject.put("success", true);
jsonObject.put("flag", flag);
jsonObject.put("msg", this.info);
return "jsonObject";
}
相信看了上段代码,大家都会明白,在ext的fileuploadfield组件中,定义了一个对象后,它会自动生成两个变量来装填这个原对象的属性,例如将组件名命名为OBJ那么它就会生成OBJFileName和OBJContentType这两个变量,而这两个变量中装的就是原文件所对应的名字和类型
注意:这三个变量一定要有getter和setter方法
相关文章推荐
- ExtJs文件上传(Ext.ux.form.FileUploadField)
- jquery ajaxfileupload文件上传类型问题
- JSP使用Common FileUpload组件实现文件上传及限制上传类型实例代码
- jquery fileupload限制上传文件类型
- IE使用ajaxFileUpload上传文件后台返回json类型前台弹出下载提示框
- ExtJS + fileuploadfield上传文件代码
- Extjs中用dwr实现文件上传时,fileuploadfield不能正常显示的问题
- Spring Boot修改最大上传文件限制:The field file exceeds its maximum permitted size of 1048576 bytes.
- ajaxFileUpload.js插件支持多文件上传的方法
- ajaxfileupload.js 文件上传
- jquery ajaxFileUpload.js插件支持多文件上传的方法
- Ajaxfileupload 上传文件后返回response的contentType错误问题
- AjaxFileUpload.js实现异步上传文件
- SpringMVC+ajaxfileupload.js实现文件无刷新上传
- 利用Jakarta commons fileupload组件实现多文件上传
- 上传文件(可用ajaxfileupload.js插件)
- jQuery插件AjaxFileUpload可以实现ajax文件上传
- 转:一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- 使用jquery-fileupload.js实现文件上传-webx3
- ajaxfileupload单文件上传兼容IE8及 input type=file样式修改