论坛首页 Web前端技术论坛

ext2.0如何做文件上传?

浏览 31573 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-24  
ext2.0的FormPanel里如何做文件上传啊?发现1.1版本里还有文件上传的API,而2.0里却没有找到相关文件上传的方法。
   发表时间:2007-12-24  
本身ext主要是做客户端的工作(它的source code就是一堆js+hmtl),而上传文件不仅仅是客户端所能完成的工作,需要服务器端有个处理类来处理上传的文件。如果ext想把upload实现的话,那么他的源代码中就要加一个UploadServlet.java了,这个java文件放在什么位置呢?;)
估计ext的作者觉得不好处理,就暂时放起了文件上传组件的制作
0 请登录后投票
   发表时间:2007-12-24  
http://extjs.com/forum/showthread.php?t=21558 看看这个
0 请登录后投票
   发表时间:2007-12-24  
form的属性里面加上fileUpload: true,
提交按钮例如:
   yourform.form.doAction('submit',{
                    url:'savePython.do',
                    method:'post',
                    params:'',
                    waitMsg:'上传中,请稍等...',
                    success:function(form,action){
                         Ext.Msg.alert('操作',"上传成功");
                         yourform.close();
                    
                     },
                    failure:function(){
                  Ext.Msg.alert('操作','上传失败!');
                    }
});

服务端返回“success:true”或者false

上传按钮没被ext包装过,不是很和谐
0 请登录后投票
   发表时间:2008-03-11  
beckrabbit 写道
form的属性里面加上fileUpload: true,
提交按钮例如:
   yourform.form.doAction('submit',{
                    url:'savePython.do',
                    method:'post',
                    params:'',
                    waitMsg:'上传中,请稍等...',
                    success:function(form,action){
                         Ext.Msg.alert('操作',"上传成功");
                         yourform.close();
                    
                     },
                    failure:function(){
                  Ext.Msg.alert('操作','上传失败!');
                    }
});

服务端返回“success:true”或者false

上传按钮没被ext包装过,不是很和谐


显示出来的效果,就没有 浏览按钮。。
  • 大小: 182.1 KB
0 请登录后投票
   发表时间:2008-03-11  
code:
    var simple = new Ext.FormPanel({
    region:'center',       
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,
        title: 'Simple Form',
        bodyStyle:'padding:5px 5px 0',
        //width: 600,
        // defaults: {width: 230},
        frame:true,
        title: '上传',
        bodyStyle:'padding:5px 5px 0',       
        autoScroll :true,
        labelWidth: 75,
        border:false,
        fileUpload: true,
        items: [
        {
                    xtype:'field',
                   // maxValue:999,
                    //minValue:-999,
                    allowBlank:false,
                    fieldLabel: '上传',
                    id:'contract',
                    disabled:false,
                    name: 'ha.contract',
                    maxLength:25,
                    anchor:'90%'
           }
        ]
       
       
       
        });
0 请登录后投票
   发表时间:2008-03-12  
Ext.onReady(function(){
    var form = new Ext.form.FormPanel({
        labelAlign: 'right',
        title: 'form',
        labelWidth: 50,
        frame:true,
        fileUpload: true,
        url: 'student?action=load',//fileUploadServlet
        width: 380,

        items: [{
            xtype: 'textfield',
            fieldLabel: '文本框',
            name: 'file',
            inputType: 'file'//文件类型
        }],
        buttons: [{
            text: '按钮',
            handler: function() {
                form.getForm().submit({
                    success: function(form, action){
                        Ext.Msg.alert('信息', action.result.msg);
                    },
                    failure: function(){
                        Ext.Msg.alert('错误', '失败');
                    }
                });
            }
        }]
    });
    form.render("form");
});

而后action中调用通用上传方法

public void upLoad(HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String name = null;
String value = null;
boolean fileFlag = false;
// 文件存放目录
String TMP_DIR = "D:\\";
File tmpFile = null;
// file name
String fName = null;

FileOutputStream baos = null;
BufferedOutputStream bos = null;
Hashtable<String, ArrayList<String>> paramHt = new Hashtable<String, ArrayList<String>>();
int BUFSIZE = 1024 * 8;
int rtnPos = 0;
byte[] buffs = new byte[BUFSIZE * 8];
// 得到请求类型
String contentType = request.getContentType();
int index = contentType.indexOf("boundary=");
String boundary = "--" + contentType.substring(index + 9);
String endBoundary = boundary + "--";
ServletInputStream sis = request.getInputStream();
System.out.println();
// 循环读取文件
while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
String strBuff = new String(buffs, 0, rtnPos);
if (strBuff.startsWith(boundary)) {

if (name != null && name.trim().length() > 0) {

if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList arrayList = (ArrayList) obj;
al = arrayList;
}
al.add(value);
paramHt.put(name, al);
}
}
name = new String();
value = new String();
fileFlag = false;
rtnPos = sis.readLine(buffs, 0, buffs.length);
if (rtnPos != -1) {

strBuff = new String(buffs, 0, rtnPos);
if (strBuff.toLowerCase().startsWith(
"content-disposition: form-data; ")) {
int nIndex = strBuff.toLowerCase().indexOf("name=\"");
int nLastIndex = strBuff.toLowerCase().indexOf("\"",
nIndex + 6);
name = strBuff.substring(nIndex + 6, nLastIndex);
}
int fIndex = strBuff.toLowerCase().indexOf("filename=\"");
if (fIndex != -1) {
fileFlag = true;
int fLastIndex = strBuff.toLowerCase().indexOf("\"",
fIndex + 10);
fName = strBuff.substring(fIndex + 10, fLastIndex);
fIndex = fName.lastIndexOf("\\");
if (fIndex == -1) {
fIndex = fName.lastIndexOf("/");
if (fIndex != -1) {
fName = fName.substring(fIndex + 1);
}
} else {
fName = fName.substring(fIndex + 1);
}
if (fName == null || fName.trim().length() == 0) {
fileFlag = false;
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
continue;
}
}
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
}
} else if (strBuff.startsWith(endBoundary)) {
System.out.println("2...");
if (name != null && name.trim().length() > 0) {
System.out.println("(name!=null)...");
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList arrayList = (ArrayList) obj;
al = arrayList;
}
al.add(value);

paramHt.put(name, al);
}
}
} else {
if (fileFlag) {
if (baos == null && bos == null) {
tmpFile = new File(TMP_DIR + fName);
baos = new FileOutputStream(tmpFile);
bos = new BufferedOutputStream(baos);
}
bos.write(buffs, 0, rtnPos);
baos.flush();
} else {
value = value + strBuff;
}
}
}

/*
* 有个问题?请求发来的时候...由于是UTF-8的编码;导致上传后的文件名是乱码
*/
System.out.println("上传文件名:" + tmpFile.getName() + "  文件大小:"
+ tmpFile.length() + "  文件路径:" + tmpFile.getPath());
response.getWriter().print("{success:true,msg:'成功'}");
}
0 请登录后投票
   发表时间:2008-03-19  
godson_2003 写道
http://extjs.com/forum/showthread.php?t=21558 看看这个


这个东西不错。。

我现在就用这个上传!!

0 请登录后投票
   发表时间:2008-03-19  
 

仿照这样,始终没有成功。

ACTION

public class StrutsFileUpload extends ActionSupport implements ServletRequestAware, Action {

private File upload;//The actual file
private String uploadContentType; //The content type of the file
private String uploadFileName; //The uploaded file name
private String fileCaption;//The caption of the file entered by user
private HttpServletRequest request;
private boolean scriptTag;

public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
} 

public String execute() throws Exception {

System.out.println("\n........... In Struts action........." + this.getFileCaption());
System.out.println("\n........... In Struts action........." + this.getUploadContentType()); 
System.out.println("\n........... In Struts action........." + this.getUploadFileName()); 

System.out.println("\n..... content type = " + request.getContentType());

HttpSession session = request.getSession();

session.setAttribute("_myContentType", "text/html");

JSONObject jsonData = new JSONObject();
jsonData.put("success", true);
jsonData.put("message", "File uploaded");


session.setAttribute("jsonObject", jsonData.toString());

return "JSON";

}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
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;
}


}


STRUTS.XML
<action name="doUpload" class="tst.StrutsFileUpload">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/png,image/gif,image/jpeg,application/octet-stream,application/pdf
</param>
<param name="maximumSize">
<!-- approx 2MB in bytes-->
2097152
</param> 
</interceptor-ref>
</action>


0 请登录后投票
   发表时间:2008-03-21  
qianlei007 写道
godson_2003 写道
http://extjs.com/forum/showthread.php?t=21558 看看这个


这个东西不错。。

我现在就用这个上传!!


我始终没有成功,能不能给一个例子参考,谢谢!
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics