码迷,mamicode.com
首页 > Web开发 > 详细

jquery ajaxform上传文件返回不提示信息的问题

时间:2015-04-14 19:29:54      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

在使用jquery的ajaxform插件进行ajax提交表单并且上传文件的时候,返回类型datatype :json但是后台通过写出一个json对象后,在执行完以后没有进入success函数,而是直接弹出一个下载窗口,将json串当下载来处理了。后来发现解决方法是,不能把json串以json的形式写出来,而是以"text/html"的格式,并把json串放到textarea标签中。

例如:

response.setContentType("text/html;charaset=utf-8");

response.getWriter().write("<textarea>"+jsonstring+"</textarea>");

这样就能够正常在js中使用json对象了。

html:

<body>
<form id="upLoadForm" action="${url}/upLoadFile" method="post">
英文检测:<input name="filePath" type="file"></input>
<button id="upload" >文件检测</button>
</form>
</body>

$(function(){ 

    $(‘#upLoadForm‘).ajaxForm({
        success : function(data, statusText, jqXHR, $form) {
        alert(data);
      }
   });
});

CheckController:

@RequestMapping(value = "/upLoadFile", method=RequestMethod.POST)
public @ResponseBody void processUploadd(HttpServletResponse response,
@RequestParam(value = "filePath") MultipartFile file,HttpSession httpSession) {

String message="没有中文!";
response.setContentType("text/html;charaset=utf-8");
try {
response.getWriter().write(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


/*String filename=file.getOriginalFilename();//得到上传文件名
if (filename == null || "".equals(filename))
{
return null;
}

try{
FileManager fileSave = new FileManager();
//获得上传路径
String filePath=FileManager.getUploadFilePath();
fileSave.SaveFileFromInputStream(file.getInputStream(),filePath,filename);
String content = FileManager.readTxtFile(filePath + "\\" + filename);
List<DataFormat> errorList = CheckEn.checkEn(content);
String info = "";
for(int i=0; i<errorList.size(); i++) {
String cn = errorList.get(i).getCn();
String pub = errorList.get(i).getErrorMessage();
info = pub + " 中文是:" + cn;
}
if(info!= "") {
message=info;
}

}
catch (Exception e)
{
e.printStackTrace();

return message;
} */
//return message;
}

jquery ajaxform上传文件返回不提示信息的问题

标签:

原文地址:http://www.cnblogs.com/beautifulFuture/p/4425649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!