在 Internet Explorer 中上传之前检查文件大小的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7279055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:36:26  来源:igfitidea点击:

Javascript to check filesize before upload in Internet Explorer

javascriptextjs

提问by user924590

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?

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

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.

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

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.

工作流程如下:用户选择要上传的文件。验证会立即开始以检查文件类型和文件大小。如果文件大小超过限制,GUI 将提示错误。如果文件大小在限制范围内,则将完整的文件路径传递给服务器端(java servlet)进行上传。

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

是否可以使用 javascript 实现文件大小检查和完整文件路径的读取?

回答by Anuj Verma

It is possible with ActiveX Objects.

ActiveX 对象是可能的。

<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>

回答by duskwuff -inactive-

There's currently no way to portably check the size of an uploaded file from the web browser. The HTML5 File APImakes this possible, but that's not available in MSIE7 -- it's currently on track for MSIE10.

目前无法从 Web 浏览器便携式检查上传文件的大小。在HTML5文件API使这成为可能,但是这并不提供MSIE7 -这是目前的轨道上MSIE10。

There is intentionally no way to determine the full path of an uploaded file, as that may include sensitive information, like the name of the end user.

故意无法确定上传文件的完整路径,因为其中可能包含敏感信息,例如最终用户的姓名。

回答by griegs

The reason you can't is because it would be bad if browsers, and javascript, could access the clients filesystem.

你不能的原因是因为如果浏览器和 javascript 可以访问客户端文件系统会很糟糕。

This is actively denied and can be seen as an attack.

这被主动拒绝,可以被视为一种攻击。

Using jQuery, Restricting File Size Before Uploading

使用 jQuery,在上传前限制文件大小

回答by Shameer Ali Shaik

Please try below code,

请尝试以下代码,

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
$("document").ready(function(){
        $("#myFile1").change(function() {

        var f1=document.getElementById("myFile1").value;
        if((f.size||f.fileSize)==09765625)
        {
        alert("file size is less than 1mb");
        }
        else
        {
        alert("file size should not exceed more than 1mb");
                $(this).val($.data(this, 'f'));
        return false;
        }

            });
});
})
</script>
</head>
<body>

<input type='file' name="file" id="myFile1" />

</body>
</html>