简体   繁体   English

无论如何要在上传前确定文件大小?

[英]Anyway to determine file size before upload?

So we're having this problem. 所以我们遇到了这个问题。 A user goes to upload a file, and if it's above 10MB, it just kind of times out the page, and clears, and no good error is thrown to describe what happened. 一个用户去上传一个文件,如果它超过10MB,它只是在页面上出现了一段时间,并且清除了,并且没有任何好的错误来描述发生了什么。 Ideally, we would like to examine the file size when the user chooses the file they want to upload but I don't know if this is even possible. 理想情况下,我们希望在用户选择要上传的文件时检查文件大小,但我不知道这是否可行。 Our framework is built with ASP.NET, VB.NET, and Javascript (and ExtJS 3.0), and it runs in IE. 我们的框架是用ASP.NET,VB.NET和Javascript(以及ExtJS 3.0)构建的,它在IE中运行。

Anyway to do this? 无论如何要做到这一点?

I don't think there's any way to do this using standard HTML forms. 我认为使用标准HTML表单无法做到这一点。

Take a look at SWFUpload . 看看SWFUpload This will let you control the file size. 这将允许您控制文件大小。

The code below works in Firefox and Chrome, but the check is omitted on IE and Opera. 下面的代码适用于Firefox和Chrome,但在IE和Opera上省略了检查。 I think in IE you may need to use an ActiveXObject. 我认为在IE中你可能需要使用ActiveXObject。 Taken and modified slightly from here . 这里稍微采取和修改。

<script type="text/javascript">
var clicked = false;
function checkSize() {
var node = document.getElementById('file');
var check = node.files[0].fileSize;
if (check > 4096)
{
alert('Your file is too big!');
 return false;
}
}
</script>

<form enctype="multipart/form-data" action="upload_file.php" method="post" class="body">
Select a file: <input type='file' id='file' name='file'>
 <input type='submit' value=' Upload File ' onClick='return checkSize()'> 
</form>

You can set the limit in web config, the property is called MaxRequestLength. 您可以在Web配置中设置限制,该属性称为MaxRequestLength。

Set it in web.config, in the httpRuntime section : 在web.config中的httpRuntime部分中设置它:

<httpRuntime executionTimeout="90" maxRequestLength="4096" /> <-- number of bytes

That should be inserted under <system.web> 那应该插在<system.web>

As for checking the size of the file, it's as easy as 至于检查文件的大小,它就像

If txtFileUpload.PostedFile.ContentLength > 1024 Then <-- bytes

Can you use or ave you tried using ActiveXObject? 您是否可以使用或尝试使用ActiveXObject?

Untested (probably won't work in IE7+) 未经测试(可能不适用于IE7 +)

function checkSize(fileInput, limit){
    if(!window.ActiveXObject){
        return false;
    }
    var oas = new ActiveXObject("Scripting.FileSystemObject"),
        d = fileInput.value,
        e = oas.getFile(d),
        f = e.size;
    return f <= limit; // in bytes
}

如果Flash是您的选项,您可以尝试swfupload

With normal javascript and the HTML file tag it is impossible to do that, because of security reasons a page cannot access the user's disk which would be required to check the file size. 使用普通的javascript和HTML文件标签是不可能的,因为安全原因,页面无法访问用户磁盘,而这需要检查文件大小。

I've seen that being done with flash but I'm not a flash guy. 我已经看到用闪光灯完成但我不是闪光灯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM