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

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

问题描述

是否可以在文件实际上传到服务器之前使用 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 构建,仅限 Internet Explorere 7 在 Windows XP 机器上使用.不允许使用 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>

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

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