//*****************************************上传的公共js***************************************************************//
/**
* 约定:types为调用时传来的参数.形式为jsp-gig-png
* uploadid为上传后要填充路径的控件id
* 上传的属性均为upload
* 功能:页面调用openUpload("","");方法即可
*/
//...允许上传的后缀名
var types = "";
//...上传后填充控件的id
var uploadid = "";
function openUpload(type,id){
types = type;
uploadid = id;
winUpload.show();
}
var formUpload = new Ext.form.FormPanel({
baseCls: 'x-plain',
labelWidth: 80,
fileUpload:true,
defaultType: 'textfield',
items: [{
xtype: 'textfield',
fieldLabel: '文 件',
name: 'upload',
inputType: 'file',
allowBlank: false,
blankText: '请上传文件',
anchor: '90%' // anchor width by percentage
}]
});
var winUpload = new Ext.Window({
title: '资源上传',
width: 400,
height:200,
minWidth: 300,
minHeight: 100,
layout: 'fit',
plain:true,
bodyStyle:'padding:5px;',
buttonAlign:'center',
items: formUpload,
buttons: [{
text: '上 传',
handler: function() {
if(formUpload.form.isValid()){
Ext.MessageBox.show({
title: 'Please wait',
msg: 'Uploading...',
progressText: '',
width:300,
progress:true,
closable:false,
animEl: 'loding'
});
formUpload.getForm().submit({
url:'uploadAction.action?types='+types,
success: function(form, action){
var objxzdz = Ext.get(uploadid).dom;
var value = action.result.msg;
objxzdz.value = value;
Ext.Msg.alert('成功','上传成功.');
winUpload.hide();
},
failure: function(form, action){
//... action生成的json{msg:上传失败},页面就可以用action.result.msg得到非常之灵活
Ext.Msg.alert('Error', action.result.msg);
}
})
}
}
},{
text: '取 消',
handler:function(){winUpload.hide();}
}]
});
//*****************************************上传的公共js***************************************************************//
现在已经封装完毕了,我们看看在页面上如何调用
openUpload("txt-xls-doc-docs-pds","xzdzid");
就这一句话,第一个参数为允许上传的类型,第二个参数为上传后地址要绑定到哪个控件(是该控件的id)
action的代码
<PRE class=java name="code">import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import net.ask123.ecommerce.common.util.Common;
import net.ask123.ecommerce.common.util.Constants;
import net.ask123.ecommerce.common.util.FileOperate;
import net.ask123.ecommerce.common.util.VeDate;
/**
* 上传的公共方法
*
* @author sam.zhang
*
*/
public class UploadAction extends ExtJsonActionSuport {
/**
*
*/
private static final long serialVersionUID = 1L;
//和前台的名字一样,这里约定是 upload
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
// 存放允许上传的后缀名
private String types;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
@SuppressWarnings("deprecation")
public String execute() throws Exception {
String msg = "";
FileOperate fo = new FileOperate();
String sid = VeDate.getNo(4);
this.savePath = "/updownFiles";
try {
// ...获取文件后缀名
String ext = fo.getFileExt(getUploadFileName());
if ("".equals(this.types)
|| Common.indexofString(this.types, "-") == -1) {
msg = "上传失败";
this.setJsonString("{success:false,msg:'" + msg + "'}");
return SUCCESS;
}
// ...判断上传的文件是否合法
boolean istrue = FileOperate.trueExt(this.types.split("-"), ext);
if (!istrue) {
msg = "您上传的文件格式不正确,正确格式为" + this.types;
this.setJsonString("{success:false,msg:'" + msg + "'}");
return SUCCESS;
}
// ...文件存放的位置
String sPath = ServletActionContext.getRequest().getRealPath(
this.getSavePath())
+ Constants.FILESPARA
+ sid.substring(0, 4)
+ Constants.FILESPARA
+ sid.substring(4, 6)
+ Constants.FILESPARA;
// ...保存在数据库的路径
String filePath = this.savePath + "/" + sid.substring(0, 4) + "/"
+ sid.substring(4, 6) + "/" + sid + "." + ext;
// 如果目录不存在则创建它
fo.createFolder(sPath);
FileOutputStream fileOutputStream = new FileOutputStream(sPath
+ sid + "." + ext);
FileInputStream fileInputStream = new FileInputStream(getUpload());
// ...
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}
this.setJsonString("{success:true,msg:'" + filePath + "'}");
} catch (Exception e) {
this.setJsonString("{success:false}");
e.printStackTrace();
}
return SUCCESS;
}
}
</PRE>
- 大小: 58.5 KB
分享到:
相关推荐
一个Ext的上传文件的控件。修改过的源码。能上传中文文件。
附件是ext上传文件的控件使用实例,下载附件后直接打开附件中的html页面,点击页面中的上传按钮就可以看到上传文件的对话框
ext上传组件。此组件使用flash技术,使界面更美观。
Ext 文件上传的完整列子,Ext文件上传。。。。
NULL 博文链接:https://landy-sun.iteye.com/blog/1005811
NULL 博文链接:https://jungh.iteye.com/blog/940900
Ext 文件上传参考文档 Ext 文件上传参考文档
ext基本的控件例子ext基本的控件例子ext基本的控件例子ext基本的控件例子ext基本的控件例子
EXT上传前先预览一下这个图片
Ext.ux.dialog Common-fileupload 整合版! <br>Ext文件上传完整包
Ext 组件:Ext初学者必备 初学宝典 Ext组件概述
Ext的gridpanel控件二次加载时丢失解决方案
Java 版Ext多文件上传Java 版Ext 文件上传
Ext上传文件例子 自己刚学ext写的例子
EXT3.0实现文件上传EXT3.0实现文件上传EXT3.0实现文件上传EXT3.0实现文件上传EXT3.0实现文件上传EXT3.0实现文件上传
文件批量上传组件,可以多个文件同时选择以及同时上传,带进度条的,可以对文件格式、大小进行限制。我上传的是一个demo,可以直接在tomcat下运行
ext文件上传下载源码
完全可运行的Ext多文件上传,带上传速率、进度条等
这是我写的一个Ext如何上传文件的 Demo 程序,你可以进我的博客看里面的一篇文章就可以. 这是前后台代码结合写的.
Ext是一个Ajax框架,可以用来开发带有华丽外观的胖客户端应用,使得我们的b/s应用更加具有活力及生命力,提高用户体验。