佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 754|回复: 10

如何filter upload file

[复制链接]
发表于 4-12-2007 11:04 PM | 显示全部楼层 |阅读模式
我的webserver 有限定我upload image file 的大小
请问要怎样才可以再submit时检查文件的大小?
我写了以下的code 但不稳定。ie 再set globalPic.src=path;后有时不能马上给答案.
我相信是应为browser 还在读取资料。用settimeout delay 一下就行了.
不过同样的东西在firefox完全不能用。不知有没有更好的办法?

<html>
<head>
<meta http-equiv="Content-Type" c />
<title>Untitled Document</title>
<script language="javascript">
var imgRe = /^.+\.(jpg|jpeg|gif|png|bmp)$/i;
var globalPic;
var timeSet;

function previewImage(pathField, previewName){
var path = pathField.value;
if (path.search(imgRe) != -1){
   globalPic=new Image();
   globalPic.src=path;
   
timeSet=setTimeout("checkSize()",300);        
}
}

function checkSize(){
  

  document.imageTest.replaceMe.src=globalPic.src;
  document.imageTest.txtSize.value = globalPic.fileSize;
  document.imageTest.txtWidth.value = globalPic.width;
  document.imageTest.txtHeight.value = globalPic.height;   
  window.clearTimeout(timeSet);
}
</script>
</head>
<body>
<form name="imageTest">
  <input type="file" name="myImage" size="30" />
  <br />size <input type="text" name="txtSize" value="" />
  <br />width <input type="text" name="txtWidth" value="" />
  <br />height <input type="text" name="txtHeight" value="" />   
  <br /><img src="clear.gif" name="replaceMe"/>
</form>
</body>
</html>

[ 本帖最后由 leomaster 于 4-12-2007 11:20 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 4-12-2007 11:14 PM | 显示全部楼层
用onchange 来触发 previewImage()?

checkSize() 加上

if(globalPic.complete)
{
    //blah blah blah
}

[ 本帖最后由 megablue 于 4-12-2007 11:21 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 4-12-2007 11:22 PM | 显示全部楼层
原帖由 megablue 于 4-12-2007 11:14 PM 发表
用onchange 来触发 previewImage()?

checkSize() 加上

if(globalPic.complete)
{
    //blah blah blah
}


我的<input type="file" name="myImage" size="30" /> 里是有 o nChange="pre viewImage(docu ment.imag eTest.myImag e,'replaceMe')"
post 上来后就不见了。还要加点space 才能出现。cari server block
回加if(globalPic.complete)试看。谢谢

[ 本帖最后由 leomaster 于 4-12-2007 11:23 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 4-12-2007 11:35 PM | 显示全部楼层
if(globalPic.complete)
好像不行ie 没反应. firefox 更死
回复

使用道具 举报

发表于 4-12-2007 11:45 PM | 显示全部楼层
setTimeout暂时放 5 秒 (5000)
在if(globalPic.complete) 前面 放 alert(globalPic.complete);

看看她有没有换去 true
回复

使用道具 举报

 楼主| 发表于 5-12-2007 12:24 AM | 显示全部楼层
function checkSize(){
if(globalPic.complete){
  alert('ok');
//  document.write("size : "+globalPic.fileSize+"<br>width : "+globalPic.width+"<br> height : "+globalPic.height);
//  document.write("<br>width : "+globalPic.width+"<br> height : "+globalPic.height);
  
   //document.write("<br>width : "+document.imageTest.replaceMe.width+"<br> height : "+document.imageTest.replaceMe.height);
   document.imageTest.replaceMe.src=globalPic.src;
   document.imageTest.txtSize.value = globalPic.fileSize;
   document.imageTest.txtWidth.value = globalPic.width;
   document.imageTest.txtHeight.value = globalPic.height;   
//  document.write("filesize : "+globalPic.fileSize);
//   alert("width "+globalPic.width);
   clearTimeout(timeSet);
}else{
  timeSet=setTimeout("checkSize()",100);         
}
//   alert("height "+globalPic.height);
}


ie ok 了。不过firefox 还是不行。好像不support globalPic.width,fileSize 何width
回复

使用道具 举报

Follow Us
发表于 5-12-2007 12:57 AM | 显示全部楼层
应该有类似的property...去google下...我很久没动Javascript了...我的javascript 很noob下的

很久以前写的debug function
  1.             function var_dump(obj)
  2.             {
  3.                 divMessageOutput = document.getElementById("debug");
  4.                
  5.                 if(divMessageOutput)
  6.                 {
  7.                     if(!obj)
  8.                     divMessageOutput.innerHTML = "null\r\n";   
  9.                     
  10.                     if("string" != typeof obj)
  11.                     {
  12.                         for(p in obj)
  13.                         {
  14.                             divMessageOutput.innerHTML += "Property: " + p + " Value: " + obj[p] + "\r\n";
  15.                             /*if(typeof obj[p] == "array" || typeof obj[p] == "object") var_dump(obj[p]);*/
  16.                         }
  17.                     }
  18.                     else
  19.                     {
  20.                         divMessageOutput.innerHTML += obj + "\r\n";
  21.                     }
  22.                 }
  23.             }
复制代码
  1. <body>
  2. <div id="debug"> </div>
复制代码



用var_dump(globalPic) 就可以看到那个object 有什么properties 了

[ 本帖最后由 megablue 于 5-12-2007 01:01 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 5-12-2007 01:24 AM | 显示全部楼层
Property: src Value: e:\Wallpaper\1.jpg
Property: complete Value: true
Property: nodeName Value: IMG
Property: nodeValue Value: null
Property: nodeType Value: 1
Property: parentNode Value: null
Property: childNodes Value: [object NodeList]
Property: firstChild Value: null
Property: lastChild Value: null
Property: previousSibling Value: null
Property: nextSibling Value: null
Property: attributes Value: [object NamedNodeMap]
Property: ownerDocument Value: [object HTMLDocument]
Property: insertBefore Value: function insertBefore() { [native code] }
Property: replaceChild Value: function replaceChild() { [native code] }
Property: removeChild Value: function removeChild() { [native code] }
Property: appendChild Value: function appendChild() { [native code] }
Property: hasChildNodes Value: function hasChildNodes() { [native code] }
Property: cloneNode Value: function cloneNode() { [native code] }
Property: normalize Value: function normalize() { [native code] }
Property: isSupported Value: function isSupported() { [native code] }
Property: namespaceURI Value: null
Property: prefix Value: null
Property: localName Value: IMG
Property: hasAttributes Value: function hasAttributes() { [native code] }
Property: tagName Value: IMG
Property: getAttribute Value: function getAttribute() { [native code] }
Property: setAttribute Value: function setAttribute() { [native code] }
Property: removeAttribute Value: function removeAttribute() { [native code] }
Property: getAttributeNode Value: function getAttributeNode() { [native code] }
Property: setAttributeNode Value: function setAttributeNode() { [native code] }
Property: removeAttributeNode Value: function removeAttributeNode() { [native code] }
Property: getElementsByTagName Value: function getElementsByTagName() { [native code] }
Property: getAttributeNS Value: function getAttributeNS() { [native code] }
Property: setAttributeNS Value: function setAttributeNS() { [native code] }
Property: removeAttributeNS Value: function removeAttributeNS() { [native code] }
Property: getAttributeNodeNS Value: function getAttributeNodeNS() { [native code] }
Property: setAttributeNodeNS Value: function setAttributeNodeNS() { [native code] }
Property: getElementsByTagNameNS Value: function getElementsByTagNameNS() { [native code] }
Property: hasAttribute Value: function hasAttribute() { [native code] }
Property: hasAttributeNS Value: function hasAttributeNS() { [native code] }
Property: id Value:
Property: title Value:
Property: lang Value:
Property: dir Value:
Property: className Value:
Property: name Value:
Property: align Value:
Property: alt Value:
Property: border Value:
Property: height Value: 0
Property: hspace Value: -1
Property: isMap Value: false
Property: longDesc Value:
Property: useMap Value:
Property: vspace Value: -1
Property: width Value: 0
Property: ELEMENT_NODE Value: 1
Property: ATTRIBUTE_NODE Value: 2
Property: TEXT_NODE Value: 3
Property: CDATA_SECTION_NODE Value: 4
Property: ENTITY_REFERENCE_NODE Value: 5
Property: ENTITY_NODE Value: 6
Property: PROCESSING_INSTRUCTION_NODE Value: 7
Property: COMMENT_NODE Value: 8
Property: DOCUMENT_NODE Value: 9
Property: DOCUMENT_TYPE_NODE Value: 10
Property: DOCUMENT_FRAGMENT_NODE Value: 11
Property: NOTATION_NODE Value: 12
Property: lowsrc Value:
Property: naturalHeight Value: 0
Property: naturalWidth Value: 0
Property: x Value: 0
Property: y Value: 0
Property: offsetTop Value: 0
Property: offsetLeft Value: 0
Property: offsetWidth Value: 0
Property: offsetHeight Value: 0
Property: offsetParent Value: null
Property: innerHTML Value:
Property: scrollTop Value: 0
Property: scrollLeft Value: 0
Property: scrollHeight Value: 0
Property: scrollWidth Value: 0
Property: clientHeight Value: 0
Property: clientWidth Value: 0
Property: tabIndex Value: -1
Property: blur Value: function blur() { [native code] }
Property: focus Value: function focus() { [native code] }
Property: style Value: [object CSSStyleDeclaration]
Property: removeEventListener Value: function removeEventListener() { [native code] }
Property: dispatchEvent Value: function dispatchEvent() { [native code] }
Property: baseURI Value: http://localhost/eagleeyes/test1.html
Property: compareDocumentPosition Value: function compareDocumentPosition() { [native code] }
Property: textContent Value:
Property: isSameNode Value: function isSameNode() { [native code] }
Property: lookupPrefix Value: function lookupPrefix() { [native code] }
Property: isDefaultNamespace Value: function isDefaultNamespace() { [native code] }
Property: lookupNamespaceURI Value: function lookupNamespaceURI() { [native code] }
Property: isEqualNode Value: function isEqualNode() { [native code] }
Property: getFeature Value: function getFeature() { [native code] }
Property: setUserData Value: function setUserData() { [native code] }
Property: getUserData Value: function getUserData() { [native code] }
Property: DOCUMENT_POSITION_DISCONNECTED Value: 1
Property: DOCUMENT_POSITION_PRECEDING Value: 2
Property: DOCUMENT_POSITION_FOLLOWING Value: 4
Property: DOCUMENT_POSITION_CONTAINS Value: 8
Property: DOCUMENT_POSITION_CONTAINED_BY Value: 16
Property: DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC Value: 32

以上是firefox的数据。width 和 heigh 都是0.fileSize 完全没有.ie的就有
回复

使用道具 举报


ADVERTISEMENT

发表于 5-12-2007 01:46 AM | 显示全部楼层


  1. 试试 globalPic.getAttribute("height");
  2. globalPic.getAttribute("weight");
复制代码
回复

使用道具 举报

 楼主| 发表于 5-12-2007 10:08 PM | 显示全部楼层
原帖由 megablue 于 5-12-2007 01:46 AM 发表


试试 globalPic.getAttribute("height";
globalPic.getAttribute("weight";



firefox 还是不行。
不知除了用javascript还有什么方法?
我唯一的方式是php
但必须submit后才能verify.本来是希望submit 前先check 的
回复

使用道具 举报

发表于 9-12-2007 02:53 AM | 显示全部楼层
你的问题好像和我某个贴类似....
我想应该是browser的security问题来的。。。。
它不让你直接access你电脑的文件。。。。。。

我建议你用旧的browser试下。。。。
可能会有效果。。。。
不过FF就不能了。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2025 12:09 AM , Processed in 0.137629 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表