现在的位置: 首页 > 综合 > 正文

struts2+extjs文件上传,提示下载问题解决

2013年09月09日 ⁄ 综合 ⁄ 共 1958字 ⁄ 字号 评论关闭

这个问题一直是新手学习extjs的问题,不知道怎么回事也许是ext源码的事也许是struts2的事,在网上搜索了很多文章,解决方法也有很多种,试了很多种都不好使,现在解决了。我现在用的extjs版本是3.2的。 用struts2+ext开发上传模块,上传文件成功,但总是在浏览器中有提示“下载”……看了一下下载的数据就是返回来的json字符串,但是前台的回调函数成功或失败函数没执行。解决方法首先是在action中直接写

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");

还有说是在Action中的返回值从SUCCESS改为NONE

public String execute() throws IOException{
       *******略******最后加上以下部分,struts配置文件正常,也不用配置text/html
,完全没有下载提示了……(不加入这个可能后续会有问题,暂时没涉及呢,涉及了再说……)
     HttpServletResponse response = ServletActionContext.getResponse();
       String msg = "{success:true}";
       response.getWriter().print(msg);
       return NONE;
}

<param name="contentType">text/html</param>这样是解决,但是解决方法很特殊,就是不让action往前台返回任何值,这样当然不会出现下载框了,可是你的提示信息都没发送到前台我怎么提示上传成功还是失败了呢,所以这个方法还是不行。

最后是在struts的配置文件中修改添加

<param name="contentType">text/html</param>

这个就好使了,追究其原因是,如果像第一种方法配置也行的,,但是最后都会被默认的contentType改写的,所以第一种方法不行了。
最后把相关重要代码贴上共参考
1.save Action中配置

 public String save(){ 
        String oldImageName = request.getParameter("oldImageName"); //是否上传过,如果存在则删除   
        if (!"noImage".equalsIgnoreCase(oldImageName)) {     
            File oldFile = new File(ServletActionContext   
                .getServletContext().getRealPath("/")   
                + "UploadImages" + File.separator+oldImageName);   
            oldFile.delete();
        }   
        System.out.println(oldImageName); //为用户新上传的图片新取一个名字     
        try {
			user.setImage(writeFile("userPhoto"));
			userService.addUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			success = false;
			return NONE;
		}
        return NONE;
    }

2.struts 配置文件中配置

<result type="json" name="none">
				<param name="contentType">text/html;charset=utf-8</param>
				<param name="excludeProperties">
				    user.myQuestionses,user.messages,user.myNotes,user.taskPapers,
				    user.tasks,user.testPapers,user.articles
  				</param>
			</result>

3.ext页面中

register:function(btn){
	    this.form.getForm().submit({
	       waitMsg: '正在加载,请稍等……',
	       waitTitle: '提示',
	       url:'json2/FileUpload_save_Json',
	       method: 'POST',
           scope:this,
	       success: function(form,action){
	           this.setUser(action.result.user.image);
	       },
	       failure: function(form, action) {
	           Ext.Msg.alert('提示', '系统出错,可能您的填写有错,请稍后再尝试上传!');
	       }
	   });
    },


抱歉!评论已关闭.