惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
F
Full Disclosure
Recorded Future
Recorded Future
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
博客园_首页
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Hacker News: Front Page
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
有赞技术团队
有赞技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
N
News and Events Feed by Topic
罗磊的独立博客
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
T
Tor Project blog
IT之家
IT之家
M
MIT News - Artificial intelligence
S
Security @ Cisco Blogs
O
OpenAI News
AI
AI
S
Securelist
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题

博客园 - 朱胜

exeCommand 阻止浏览器事件传递 Ext js Grouping 展开和折叠方法 JS获取上传文件的绝对路径,兼容IE和FF - 朱胜 - 博客园 asp.net2.0中异步调用WebService - 朱胜 - 博客园 ASP.Net的正则表达式(RegularExpression) Extjs简介(二) Extjs框架简介(一) ASP.NET 压缩和解压缩 - 朱胜 - 博客园 ASP.NET 恢复备份Sql server - 朱胜 WPF学习2:布局(上) WPF学习笔记1 WPF生命周期和参数配置 WPF编程笔记 0:WPF概况(上部分) clientHeight,offsetHeight和scrollHeight区别 CSS HACK(ie6-ie7-fox 兼容) 页面禁止后退的方法 upLoad控件设置禁止输入的方法 - 朱胜 - 博客园 [ASP.NET] 实现Label自动换行 - 朱胜 - 博客园 js调用web服务范例 - 朱胜 - 博客园
JavaScript判断文件大小 - 朱胜 - 博客园
朱胜 · 2009-08-18 · via 博客园 - 朱胜

第一种是应用ActiveX控件的实现,例如:

Javascript代码
<script type="text/javascript">   
function getFileSize(filePath)   
{   
   var fso = new ActiveXObject("Scripting.FileSystemObject");   
   alert("文件大小为:"+fso.GetFile(filePath).size);   
}   
</script>   
<body>   
<INPUT TYPE="file" NAME="file" SIZE="30" onchange="getFileSize(this.value);">   
</body> 
<script type="text/javascript">
function getFileSize(filePath)
{
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   alert("文件大小为:"+fso.GetFile(filePath).size);
}
</script>
<body>
<INPUT TYPE="file" NAME="file" SIZE="30" onchange="getFileSize(this.value);">
</body>


这种方法可以实现,也容易被开发人员想到,但是唯一不足之处是有安全提示,当然把文件名改为.hta则会屏蔽掉安全提示,但很难被需求所取。不推荐,也不多说

下面主要谈谈另一种方式,在html标签中有一个不为一般开发人员“深”知的img标签,先来说下他有的属性:src,dynsrc,start,alt,controls,loop,loopdelay,hspace,vspace....还有一些常用的属性就不列出来了,在这里我们说一下"dynsrc"这个属性:dynsrc可以用来插入各种多媒体,格式可以是Wav、Avi、AIFF、AU、MP3、Ra、Ram等等。url为音频或视频文件及其路径,可以是相对路径或绝对路径。

示例:<img dynsrc="xxxx.mp3">

这样我们就可以根据dynsrc动态赋值任何类型文件的路径,在javascript中根据Image对象本身的fileSize属性来得到文件的大小。当然Image对象还有其他的几个属性,例如:fileCreatedDate、fileModifiedDate、fileSize、fileUpdatedDate、filters... , 代码如下:


Javascript代码
<script type="text/javascript">   
function getFileSize(filePath)   
{   
   var image=new Image();   
   image.dynsrc=filePath;   
   alert(image.fileSize);   
}   
</script>   
<body>   
<INPUT TYPE="file" NAME="file" SIZE="30" onchange="getFileSize(this.value)">   
</body> 
<script type="text/javascript">
function getFileSize(filePath)
{
   var image=new Image();
   image.dynsrc=filePath;
   alert(image.fileSize);
}
</script>
<body>
<INPUT TYPE="file" NAME="file" SIZE="30" onchange="getFileSize(this.value)">
</body>