如何在struts2下利用ajax实现上传图片的大小检测,并弹出警告,最好有具体代码,谢谢
[b]问题补充:[/b]
Chris_Lu 你验证过这个JS么,这个是错误的,起码在我机器上执行不了这个功能
另外,我想,能不能异步的验证文件大小,因为如果把文件上传到服务器再验证,时间可能会比较长,如果文件大点的话
[b]问题补充:[/b]
Chris_Lu 能留个QQ么,交流下
JS要校验的话需要ActiveX支持,因为毕竟是要读取到上传文件的。但是有了ActiveX就限制了客户端。
个人建议是把文件传到服务器,然后再从服务器获取文件大小,这种也有一个问题,文件不能过大。
看LZ的选择了。
基于Struts2的话使用uploadInterceptor就可以上传文件并在Action中就可以拿到文件大小了,LZ的Ajax也走Action就可以了。
代码我这里没有现成的,不好意思
就是在iframe中动态创建form提交,后台解析上传的图片大小,再返回结果,
这只能用iframe的方法,ajax不能发送文件流
检查图片大小可以用js来写
var bname = navigator.appName;
//判断浏览器类型
if (bname.search(/netscape/i) == 0){
if(document.getElementById("upload").files.item(0).fileSize>512000){
alert("图片尺寸请不要大于500KB");
return false;
}
} else if (bname.search(/microsoft/i) == 0){
var location = document.getElementById("upload").value;
var point = location.lastIndexOf(".");
var type = location.substr(point);
img=document.createElement("img");
img.src=location;
if(img.fileSize>512000){
alert("图片尺寸请不要大于500KB");
return false;
}
}
没问题啊
var bname = navigator.appName;
if (bname.search(/netscape/i) == 0){
if(document.getElementById("upload").files.item(0).fileSize>512000){
alert("Logo图片尺寸请不要大于500KB");
}
} else if (bname.search(/microsoft/i) == 0){
var location = document.getElementById("upload").value;
var point = location.lastIndexOf(".");
var type = location.substr(point);
img=document.createElement("img");
img.src=location;
if(img.fileSize>512000){
alert("Logo图片尺寸请不要大于500KB");
}
}
jsp:
js:
function chkForm(){
var bname = navigator.appName;
if (bname.search(/netscape/i) == 0){
if(document.getElementById("upload").files.item(0).fileSize>512000){
alert("Logo图片尺寸请不要大于500KB");
}else {
return true;
}
} else if (bname.search(/microsoft/i) == 0){
var location = document.getElementById("upload").value;
var point = location.lastIndexOf(".");
var type = location.substr(point);
img=document.createElement("img");
img.src=location;
if(img.fileSize>512000){
alert("Logo图片尺寸请不要大于500KB");
}else {
return true;
}
}
return false;
}