●●●JS限制上传文件大小的问题:为什么取得的fileSize总是-1??●●●

Paradise_heida 2007-11-15 02:02:26

图片文件上传,控制上传文件大小的问题:为什么取得的fileSize总是-1??


<form>
<INPUT id="filSelect" style="WIDTH: 500px; HEIGHT: 22px" onpropertychange="if(this.value!='') {document.frmVCM310.txtHidFileName.value = this.value.split('\\')[this.value.split('\\').length-1];}"
type="file" size="109" name="filSelect" runat="server">
</form>

var img = null;
    var location = document.frmVCM310.filSelect.value;
    var point = location.lastIndexOf(".");
    var type = location.substr(point).toUpperCase();

if(type == ""){
return true;
}
else if( type == ".JPEG" || type == ".JPG" || type == ".GIF" ){

img = document.createElement("img");
img.src = location;
if(img.fileSize > 2 * 1024 * 1024 ){ // <-----上传文件上限2M,在此出错。取得的fileSize总是-1??

alert("Error1");
return false;
}
else{
return true;
}
}
else
{
alert("Error2");
return false;
}


请各位相助!!
分不够还有!!

...全文
2606 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangguiming 2010-12-20
  • 打赏
  • 举报
回复
我也遇到啊,为什么???
pipi2142001 2008-07-22
  • 打赏
  • 举报
回复
var img_new = document.createElement("img");
var fileSize = img_new.fileSize;
//让图片能完全加载完,才能取到其size
img_new.onload = function(){
fileSize=this.fileSize;
}

我也遇到一样的问题,这个可以解决!
Paradise_heida 2007-11-15
  • 打赏
  • 举报
回复
多谢楼上的回答。
你的方法试验过了,还是不行。。。
shineran 2007-11-15
  • 打赏
  • 举报
回复
img=document.createElement("img");
当执行到这一句时,如果图片的文件大于10KB的话,是需要一点加载时间才能正确的获取img.fileSize的值。


<script>
var img = null;
function getsize()
{
var local = document.getElementById("filSelect").value;
var point = local.lastIndexOf(".");
var type = local.substr(point).toUpperCase();

if(type == ""){
return true;
}
else if( type == ".JPEG" || type == ".JPG" || type == ".GIF" )
{

img = document.createElement("img");
img.src = local;
alert(img.fileSize);//立即获取时为-1
setTimeout("alert(img.fileSize);",1000);//延迟一定时间后再获取,得到正确的大小值
if(img.fileSize > 2*1024*1024 )
{

alert("Error1");
return false;
}
else
{
return true;
}
}
else
{
alert("Error2"); return false;
}
}
</script>
tantaiyizu 2007-11-15
  • 打赏
  • 举报
回复


确认IMG 路径正确?
1.在过去一年,小编写过一个批量上传图片的例子,那个例子是基于百度编辑器改造的,用起来必须依赖百度编辑器,所以小编就又弄了一个,就是今天要介绍的了,uploadify上传的了前端用flash,javascript编写,后端兼容java,php,.net等语言 2.首先在把“uploadify”文件夹放入项目中 3.页面上面引入 <script type="text/javascript" src="resource/js/uploadify/main.js">js中写入以下配置: $(document).ready(function(){ var setting={ "id": "uploadify",//绑定的input的ID "swf": 'resource/js/uploadify/uploadify.swf',//[必须设置]swf的路径 "uploader": "site/txtOrTopicImageUpload!txtOrTopicImageUpload.action?uid="+uid,//[必须设置]上传文件触发的url "auto":true,//文件选择完成后,是否自动上传 "buttonText":'+上传图片',//上传按钮的文字 "height": 40,//上传按钮的高和宽 "width": 100, "buttonCursor": 'pointer',//上传鼠标hover后Cursor的形状 //"cancelImage": "resource/js/uploadify/uploadify-cancel.png",//[必须设置]取消图片的路径 //"checkExisting":'/uploader/uploadify-check-existing.php',//检查上传文件是否存,触发的url,返回1/0 "debug": false,//debug模式开/关,打开后会显示debug时的信息 "fileObjName":'file', "fileSizeLimit" : 3*1024*1024,//文件的极限大小,以字节为单位,0为不限制。1MB:1*1024*1024 "fileTypeDesc": '图片',//允许上传的文件类型的描述,在弹出的文件选择框里会显示 "fileTypeExts": '*.jpg;*.bmp;*.gif;*.png',//允许上传的文件类型,限制弹出文件选择框里能选择的文件 "method": 'post',//和后台交互的方式:post/get "multi": true,//是否能选择多个文件 "queueID": 'None_none',//显示上传文件队列的元素id,可以简单用一个div来显示 "queueSizeLimit" : 10,//队列中允许的最大文件数目 "progressData" : 'all', // 'percentage''speed''all'//队列中显示文件上传进度的方式:all-上传速度+百分比,percentage-百分比,speed-上传速度 "removeCompleted" : true,//上传成功后的文件,是否在队列中自动删除 "removeTimeout": 0, "requeueErrors" : true, "postData": {},//和后台交互时,附加的参数 "preventCaching" : true, "transparent": true, "successTimeout" : 30,//上传时的timeout "uploadLimit":0,//能同时上传的文件数目 "onUploadSuccess" : function(file,data,response) {//上传完成时触发(每个文件触发一次) var a =eval("("+data+")");   imgArray[imageIndex]=a.result; showImges(imageIndex); $("#loading").hide(); }, "onDialogClose": function(queueData){ var result=countArray(imgArray),a=queueData.queueLength; if(a+result>image_size){ jDialog("一次最多上传10张图片!"); for(var s in queueData.files){ $("#uploadify").uploadify("cancel",s); } return; }if(a!=0){ $("#loading").show(); } } }; $("#uploadify").uploadify(setting); }); 5.下面是参数的详细配置: 使用: //绑定的界面元素 $("#gallery").uploadify({ 设置参数,参数如下. }); 设置的属性: id: jQuery(this).attr('id'),//绑定的input的ID swf: 'http://www.static-xxx.nu/uploader/uploadify.swf',//[必须设置]swf的路径 uploader: '/uploadify/galleri.php',//[必须设置]上传文件触发的url auto:false,//文件选择完成后,是否自动上传 buttonText:'Välj Filer',//上传按钮的文字 height: 30,//上传按钮的高和宽 width: 120, buttonCursor: 'pointer',//上传鼠标hover后Cursor的形状 cancelImage: 'http://www.static-xxx.nu/uploadify-cancel.png',//[必须设置]取消图片的路径 checkExisting:'/uploader/uploadify-check-existing.php',//检查上传文件是否存,触发的url,返回1/0 debug: true,//debug模式开/关,打开后会显示debug时的信息 fileObjName:'file', fileSizeLimit : 0,//文件的极限大小,以字节为单位,0为不限制。1MB:1*1024*1024 fileTypeDesc: 'Bild JPG',//允许上传的文件类型的描述,在弹出的文件选择框里会显示 fileTypeExts: '*.jpg',//允许上传的文件类型,限制弹出文件选择框里能选择的文件 method: 'post',//和后台交互的方式:post/get multi: true,//是否能选择多个文件 queueID: 'fileQueue',//显示上传文件队列的元素id,可以简单用一个div来显示 queueSizeLimit : 999,//队列中允许的最大文件数目 progressData : 'all', // 'percentage''speed''all'//队列中显示文件上传进度的方式:all-上传速度+百分比,percentage-百分比,speed-上传速度 removeCompleted : true,//上传成功后的文件,是否在队列中自动删除 removeTimeout: 3, requeueErrors : true, postData: {},//和后台交互时,附加的参数 preventCaching : true, transparent: true, successTimeout : 30,//上传时的timeout uploadLimit:999//能同时上传的文件数目 设置的事件: onDialogClose : function(swfuploadifyQueue) {//当文件选择对话框关闭时触发   if( swfuploadifyQueue.filesErrored > 0 ){   alert( '添加至队列时有'   +swfuploadifyQueue.filesErrored   +'个文件发生错误n'   +'错误信息:'   +swfuploadifyQueue.errorMsg   +'n选定的文件数:'   +swfuploadifyQueue.filesSelected   +'n成功添加至队列的文件数:'   +swfuploadifyQueue.filesQueued   +'n队列中的总文件数量:'   +swfuploadifyQueue.queueLength);   } } onDialogOpen : function() {//当选择文件对话框打开时触发   alert( 'Open!'); } onSelect : function(file) {//当每个文件添加至队列后触发   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus); } onSelectError : function(file,errorCode,errorMsg) {//当文件选定发生错误时触发   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus   + ' - 错误代码: ' + errorCode   + ' - 错误信息: ' + errorMsg); } onQueueComplete : function(stats) {//当队列中的所有文件全部完成上传时触发   alert( '成功上传的文件数: ' + stats.successful_uploads   + ' - 上传出错的文件数: ' + stats.upload_errors   + ' - 取消上传的文件数: ' + stats.upload_cancelled   + ' - 出错的文件数' + stats.queue_errors); } onUploadComplete : function(file,swfuploadifyQueue) {//队列中的每个文件上传完成时触发一次   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus   + ' - 出错的文件数: ' + swfuploadifyQueue.filesErrored   + ' - 错误信息: ' + swfuploadifyQueue.errorMsg   + ' - 要添加至队列的数量: ' + swfuploadifyQueue.filesSelected   + ' - 添加至对立的数量: ' + swfuploadifyQueue.filesQueued   + ' - 队列长度: ' + swfuploadifyQueue.queueLength); } onUploadError : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {//上传文件出错是触发(每个出错文件触发一次)   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus   + ' - 错误代码: ' + errorCode   + ' - 错误描述: ' + errorMsg   + ' - 简要错误描述: ' + errorString   + ' - 出错的文件数: ' + swfuploadifyQueue.filesErrored   + ' - 错误信息: ' + swfuploadifyQueue.errorMsg   + ' - 要添加至队列的数量: ' + swfuploadifyQueue.filesSelected   + ' - 添加至对立的数量: ' + swfuploadifyQueue.filesQueued   + ' - 队列长度: ' + swfuploadifyQueue.queueLength); } onUploadProgress : function(file,fileBytesLoaded,fileTotalBytes, queueBytesLoaded,swfuploadifyQueueUploadSize) {//上传进度发生变更时触发 alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus   + ' - 当前文件已上传: ' + fileBytesLoaded   + ' - 当前文件大小: ' + fileTotalBytes   + ' - 队列已上传: ' + queueBytesLoaded   + ' - 队列大小: ' + swfuploadifyQueueUploadSize); } onUploadStart: function(file) {//上传开始时触发(每个文件触发一次)   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus ); } onUploadSuccess : function(file,data,response) {//上传完成时触发(每个文件触发一次)   alert( 'id: ' + file.id   + ' - 索引: ' + file.index   + ' - 文件名: ' + file.name   + ' - 文件大小: ' + file.size   + ' - 类型: ' + file.type   + ' - 创建日期: ' + file.creationdate   + ' - 修改日期: ' + file.modificationdate   + ' - 文件状态: ' + file.filestatus   + ' - 服务器端消息: ' + data   + ' - 是否上传成功: ' + response); }

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧