您的位置:首页 > 编程语言 > Java开发

ajaxfileupload.js在SpringMVC中使用笔记

2015-06-09 17:39 507 查看
1.页面代码

<input type="file" id="file" value="" name="file" value="请选择" width="60px" onchange="changePhoto()" />

2.js

   function ajaxFileUpload(file){

         $.ajaxFileUpload(

          {
         url:'${pageContext.request.contextPath}/file.do?method=fileUpload',            //需要链接到服务器地址
         secureuri:false,
         fileElementId:'file',//文件选择框的id属性
         type:'post',                     
         dataType: 'json', 
         data:{file: file},                          //服务器获取文件的名称
         success: function (data, status)            
        {   
       
 console.debug("添加成功"+data.result+","+status);
         },
         error: function (data, status, e)            
         {
            alert("添加失败!");
         }

        });

         

      

    }
function changePhoto(){
var file =$("#photoPath_view").val();
ajaxFileUpload(file);
}

3.上传结果

       //返回上传结果
String res = "{ status:'" +1 + "', result:'" + fileNum + "',imgurl:'" + path + "'}";

        response.getWriter().write(res);

总结:1.from中不能嵌套form;

             2.使用ajaxfileupload.js时会引用jquery中的handleError(),但是版本大于1.4.2的jquery中取消了该方法.

           解决方法:(1).使用低版本的jquery;

                              (2)将下列代码加入ajaxfileupload.js中;

                  handleError: function(
s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) {
s.error.call( s.context || s, xhr, status, e );
}

// Fire the global callback
if ( s.global ) {
(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
}
},
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息