Javascript在Internet Explorer中上传前检查文件大小 [英] Javascript to check filesize before upload in Internet Explorer

查看:116
本文介绍了Javascript在Internet Explorer中上传前检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实际上传到服务器之前,是否可以使用Javascript来检查文件大小(在客户端)?

Is it possible to use Javascript to check for a file's size (at the client side) before it is actually uploaded to the server?

该应用程序是基于EXTJS和Java,仅限Windows XP机器上的Internet Explorere 7使用。不允许使用activeX。

The application is built on EXTJS and Java and is restricted to usage by Internet Explorere 7 on Windows XP machines. No usage of activeX is allowed.

工作流是这样的:
用户选择要上传的文件。
验证立即启动检查文件类型和文件大小。
如果文件大小超过限制,则GUI将提示错误。
如果文件大小在限制之内,则完整的文件路径将被传递到要上传的服务器端(java servlet)。

The workflow is as such: User selects a file to upload. Validation kicks in immediately to check for file type and filesize. If filesize exceeds limit, the GUI will prompt with error. If filesize is within the limit, the full filepath will be passed to the server end (java servlet) to be uploaded.

文件大小检查和读取完整的文件路径可以通过javascript实现?

Is the filesize check and reading of full file path possible to be achieved with javascript?

推荐答案

可以使用ActiveX对象。

It is possible with ActiveX Objects.

<html>
<head>
<script>
function getSize()
{
    var myFSO = new ActiveXObject("Scripting.FileSystemObject");
    var filepath = document.upload.file.value;
    var thefile = myFSO.getFile(filepath);
    var size = thefile.size;
    alert(size + " bytes");
}
</script>
</head>
<body>
<form name="upload">
<input type="file" name="file">
<input type="button" value="Size?" onClick="getSize();">
</form>
</body>
</html>

这篇关于Javascript在Internet Explorer中上传前检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆