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] );
}
},
<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] );
}
},
相关文章推荐
- 【转载】Java 7之基础 - 强引用、弱引用、软引用、虚引用
- 下压堆栈的链表实现 java版本
- java 语法 java没学好,休想学好安卓!
- java用流读写文件
- Java foreach语句
- Java最佳实战
- Myeclipse常用快捷键
- spring 第一篇(1-3):鸟瞰spring蓝图
- java线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用
- java线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用
- java线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用
- Java .class文件是什么?
- 《深入理解Java虚拟机》学习小记一之自动内存管理机制(三)
- 《深入理解Java虚拟机》学习小记一之自动内存管理机制(二)
- java final keyword
- Java for LeetCode 209 Minimum Size Subarray Sum
- Swig编译C代码供java调用实例
- Spring中使用Hessian暴露服务
- 《深入理解Java虚拟机》学习小记一之自动内存管理机制(一)
- java.net.SocketException: select failed