首页 新闻 会员 周边 捐助

commons-fileupload-1.2.jar加Ext.ux.UploadDialog.js做上传的问题,求高手帮忙

0
悬赏园豆:5 [已解决问题] 解决于 2012-11-21 23:16

用commons-fileupload-1.2.jar加Ext.ux.UploadDialog.js做上传,结果文件是上传成功了,但UploadDialog.js控件提示的是上传失败,在网上查了好久,都说要返回一个json给UploadDialog.js,但是我太菜鸟了,没搞明白怎么返回这个值。

到底jsp要怎么样才能返回那个值供uploadDialog.js回掉呢?

 




贴出我的代码


调用的jsp代码:

上传附件:
<input type="file">
<input type="button" id="btnUplod" onclick="uploadSw()" value="上传附件">

js代码:

function uploadSw() {
    debugger;
    dialog = new Ext.ux.UploadDialog.Dialog({   
        title: '文件上传' ,   

        url:'fileUpload.jsp' , 
        post_var_name:'files', 
        width : 450,   
        height : 300,   
        minWidth : 450,   
        minHeight : 300,   
        draggable : true ,   
        resizable : true ,   
        // autoCreate: true,
        constraintoviewport: true ,   
        // permitted_extensions:[ 'JPG' , 'jpg' , 'jpeg' , 'JPEG' , 'GIF' ,
        // 'gif' , 'png' , 'PNG' ],
        modal: true ,   
        reset_on_hide: false ,   
        allow_close_on_upload: false ,    // 关闭上传窗口是否仍然上传文件
        // upload_autostart: true //是否自动上传文件
        upload_autostart: false  
  });  
    
  dialog.show();         
  dialog.on( 'uploadsuccess' , onUploadSuccess); // 定义上传成功回调函数
  dialog.on( 'uploadfailed' , onUploadFailed); // 定义上传失败回调函数
  dialog.on( 'uploaderror' , onUploadFailed); // 定义上传出错回调函数
  dialog.on( 'uploadcomplete' , onUploadComplete); // 定义上传完成回调函数
    
}
 
// 文件上传成功后的回调函数
onUploadSuccess = function(dialog, filename, resp_data, record){     
   if(!resp_data.success){  
       alert(resp_data.message);// resp_data是json格式的数据
   }          
};     
    
// 文件上传失败后的回调函数
onUploadFailed = function(dialog, filename, resp_data, record){     
   alert(resp_data.message);     
};     
    
// 文件上传完成后的回调函数
onUploadComplete = function(dialog){       
  dialog.hide();     
};     

jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");    
     String uploadPath = "E:\\temp";
    String path = request.getContextPath();
    String strfilename="";
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart == true) {
        try {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);//得到所有的文件
            Iterator<FileItem> itr = items.iterator();
            while (itr.hasNext()) {//依次处理每个文件
                FileItem item = (FileItem) itr.next();
                String fileName = item.getName();//获得文件名,包括路径
                if (fileName != null) {
                    File fullFile = new File(item.getName());
                    File savedFile = new File(uploadPath, fullFile.getName());
                    strfilename = fullFile.getName();
                    item.write(savedFile);
                }
                out.print("'success':true,'fileName':'"+fileName+"','message':'success!'");
            }
            out.print("upload succeed");
            out.print("'success':true,'fileName':'fileName','message':'success!'");
            response.getWriter().print("{success:true,message:'上传成功'}");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    else {
        out.print("'success':true,'fileName':'fileName','message':'success!'");
        response.getWriter().print("{success:true,message:'上传成功'}");
    }
%>
</body>
</html>
逊王之王的主页 逊王之王 | 初学一级 | 园豆:9
提问于:2012-07-29 01:38
< >
分享
最佳答案
0
[code=Java]
//我自己搞定了,用IEWatch调啊调,调啊调,终于找到了,原来是输出的不是纯json,而是带有html代码,删除所有html代码就OK了



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="com.boco.bomc.framework.web.ui.*"%>
<%@ page import="com.boco.bomc.base.bean.UserBean"%>
<%@ page import="com.boco.bomc.unicom.guizhou.job_gz.bean.*"%>
<%@ page import="com.boco.bomc.unicom.guizhou.job_gz.*"%>
<%
    request.setCharacterEncoding("UTF-8");    
    
    String uploadPath = "E:\\temp";
    String path = request.getContextPath();
    String strfilename="";
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart == true) {
        try {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);//得到所有的文件
            Iterator<FileItem> itr = items.iterator();
            while (itr.hasNext()) {//依次处理每个文件
                FileItem item = (FileItem) itr.next();
                String fileName = item.getName();//获得文件名,包括路径
                if (fileName != null) {
                    File fullFile = new File(item.getName());
                    File savedFile = new File(uploadPath, fullFile.getName());
                    strfilename = fullFile.getName();
                    item.write(savedFile);
                }
                //System.out.println("1---------------{success:true,message:'上传成功'}");
                //response.resetBuffer();
                //response.getWriter().print("{success:true,message:'上传成功'}"); 
            }
            System.out.println("{2--------------success:true,message:'上传成功'}");
            response.resetBuffer();
            //response.reset();
            //response.getWriter().print("{success:true,message:'上传成功'}"); 
            out.print("{success:true,message:'上传成功'}");
        }
        catch (Exception e) {
            e.printStackTrace();
            //response.resetBuffer();
            //System.out.println("3-----------{success:true,message:'上传成功'}");
        }
    }
    else {
        //System.out.println("4----------------------{success:true,message:'上传成功'}");
        //response.resetBuffer();
        //response.getWriter().print("{success:true,message:'上传成功'}");
    }
%>
[/code]
逊王之王 | 初学一级 |园豆:9 | 2012-07-29 17:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册