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

推荐订阅源

T
Tailwind CSS Blog
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
Help Net Security
Help Net Security
月光博客
月光博客
N
News and Events Feed by Topic
Cloudbric
Cloudbric
博客园 - 司徒正美
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
The Register - Security
The Register - Security
The Hacker News
The Hacker News
I
InfoQ
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
F
Fortinet All Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
C
Cisco Blogs
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Latest news
Latest news
PCI Perspectives
PCI Perspectives
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
T
Tor Project blog
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Webroot Blog
Webroot Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 朱胜

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>