关于使用jQuery-form.js上传文件的注意事项

1、JS端调用如下(http://plugins.jquery.com/form/):

$("form#upload").ajaxSubmit({  
     url: "../excel/upload.action",  
     type: "POST",  
     resetForm : true, 
     forceSync: true,
     success:function(data) {  
          // TODO
     },  
     error:function(xhr, textStatus, errorThrown){  
         // TODO  
     }  
 }); 
注:a、设置返回参数类型dataType为html或不做设置;

       b、服务器端偶现接收不到提交的请求,需将forceSync设置为true,去除请求延迟(IE8、IE9);

2、JAVA服务器端:

@RequestMapping(value = "/upload", method = {RequestMethod.POST}, produces = MediaType.TEXT_HTML_VALUE)  
public void upload(HttpServletRequest request, HttpServletResponse response, 
    @RequestParam(value = "file") MultipartFile file) throws Exception  
{   
    // code here
		
    response.getWriter().write("{\"code\": 0, \"desc\":\"上传成功\"}"); 
}
注:a、返回的内容类型必须设置为“text/html”,否则IE浏览器端将会提示下载操作;


你可能感兴趣的:(JAVA,Web前端)