后台返回json数据,提示下载保存

这个问题有点烦,问题解决了,但道理没有弄明白转载一个最有帮助的文章吧http://guofengcn.iteye.com/blog/655370
用struts2+ext开发上传模块,上传文件成功,但总是在浏览器中有提示“下载”……

而且发现前台的success和failure都没有执行到,打印json串一切正常……



这就奇怪了~~~上网找原因……发现好多人遇到这种问题,按网上的说法一步一步的试,首先是在struts配置文件中加入:

Xml代码
1.<param name="contentType">text/html</param>
<param name="contentType">text/html</param> 还是没解决掉……



换个方式,在action中直接写:

Java代码
1.HttpServletResponse response = ServletActionContext.getResponse();
2.response.setContentType("text/html;charset=UTF-8");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
试了下,还是不行……

还有说把struts配置文件中的:<result type="json">中的json改为XXX的……没有去尝试!



说正题,解决方式~

经过无数次的尝试后发现,其实只要将Action中的返回值从SUCCESS改为NONE,并写……

Java代码
1.public String execute() throws IOException{
2. *******略******最后加上以下部分,struts配置文件正常,也不用配置text/html
3.,完全没有下载提示了……(不加入这个可能后续会有问题,暂时没涉及呢,涉及了再说……)
4. HttpServletResponse response = ServletActionContext.getResponse();
5. String msg = "{success:true}";
6. response.getWriter().print(msg);
7. return NONE;
8.}
public String execute() throws IOException{
*******略******最后加上以下部分,struts配置文件正常,也不用配置text/html
,完全没有下载提示了……(不加入这个可能后续会有问题,暂时没涉及呢,涉及了再说……)
HttpServletResponse response = ServletActionContext.getResponse();
String msg = "{success:true}";
response.getWriter().print(msg);
return NONE;
} 到现在为止,下载问题应该不会再出现了……



谁能有更好的解决方式麻烦告诉我一下……


以下是二楼的回复,也很有帮助
我试了你的第一个方法还解决了:firfox不再弹出下载对话框,序列化的action照样能接受,Ext form提交后的回调函数正常执行,谢了,呵呵
不知道你为什么没成功,我把重要代码,提出来,你自己对照看看,说不定能有所收获:
1.save action方法:

Java代码
1.public String save(){
2. String oldImageName = request.getParameter("oldImageName"); //是否上传过,如果存在则删除
3. if (!"noImage".equalsIgnoreCase(oldImageName)) {
4. File oldFile = new File(ServletActionContext
5. .getServletContext().getRealPath("/")
6. + "UploadImages" + File.separator+oldImageName);
7. oldFile.delete();
8. }
9. System.out.println(oldImageName); //为用户新上传的图片新取一个名字
10. try {
11. user.setImage(writeFile("userPhoto"));
12. userService.addUser(user);
13. } catch (Exception e) {
14. e.printStackTrace();
15. message = e.getMessage();
16. success = false;
17. return NONE;
18. }
19. return NONE;
20. }
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.action配置:

Java代码
1.<result type="json" name="none">
2. <param name="contentType">text/html;charset=utf-8</param>
3. <param name="excludeProperties">
4. user.myQuestionses,user.messages,user.myNotes,user.taskPapers,
5. user.tasks,user.testPapers,user.articles
6. </param>
7. </result>
<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 form提交

Java代码
1.register:function(btn){
2. this.form.getForm().submit({
3. waitMsg: '正在加载,请稍等……',
4. waitTitle: '提示',
5. url:'json2/FileUpload_save_Json',
6. method: 'POST',
7. scope:this,
8. success: function(form,action){
9. this.setUser(action.result.user.image);
10. },
11. failure: function(form, action) {
12. Ext.Msg.alert('提示', '系统出错,可能您的填写有错,请稍后再尝试上传!');
13. }
14. });
15. },
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('提示', '系统出错,可能您的填写有错,请稍后再尝试上传!');
}
});
},


你在return的时候都是返回的NONE,所以当然没有问题,我是想正常返回success,你试试把return设置成success,看会不会继续出下载的情况
1 楼 wkq_361138880 2010-05-21 引用
我试了你的第一个方法还解决了:firfox不再弹出下载对话框,序列化的action照样能接受,Ext form提交后的回调函数正常执行,谢了,呵呵
不知道你为什么没成功,我把重要代码,提出来,你自己对照看看,说不定能有所收获:
1.save action方法:

Java代码
1.public String save(){
2. String oldImageName = request.getParameter("oldImageName"); //是否上传过,如果存在则删除
3. if (!"noImage".equalsIgnoreCase(oldImageName)) {
4. File oldFile = new File(ServletActionContext
5. .getServletContext().getRealPath("/")
6. + "UploadImages" + File.separator+oldImageName);
7. oldFile.delete();
8. }
9. System.out.println(oldImageName); //为用户新上传的图片新取一个名字
10. try {
11. user.setImage(writeFile("userPhoto"));
12. userService.addUser(user);
13. } catch (Exception e) {
14. e.printStackTrace();
15. message = e.getMessage();
16. success = false;
17. return NONE;
18. }
19. return NONE;
20. }
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.action配置:

Java代码
1.<result type="json" name="none">
2. <param name="contentType">text/html;charset=utf-8</param>
3. <param name="excludeProperties">
4. user.myQuestionses,user.messages,user.myNotes,user.taskPapers,
5. user.tasks,user.testPapers,user.articles
6. </param>
7. </result>
<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 form提交

Java代码
1.register:function(btn){
2. this.form.getForm().submit({
3. waitMsg: '正在加载,请稍等……',
4. waitTitle: '提示',
5. url:'json2/FileUpload_save_Json',
6. method: 'POST',
7. scope:this,
8. success: function(form,action){
9. this.setUser(action.result.user.image);
10. },
11. failure: function(form, action) {
12. Ext.Msg.alert('提示', '系统出错,可能您的填写有错,请稍后再尝试上传!');
13. }
14. });
15. },


自己的实验<param name="contentType">text/html</param> 是好用的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值