1.页面代码
<input type="file" id="file" value="" name="file" value="请选择" width="60px" οnchange="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] );
}
},