admin管理员组文章数量:1290998
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. 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.
Anyway to do this?
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. 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.
Anyway to do this?
Share Improve this question asked Jul 8, 2010 at 19:47 ScottScott 431 silver badge5 bronze badges 1- possible duplicate of How do you restrict the size of a file being uploaded with JavaScript (or Java) without transferring the entire file? – Brian Commented Apr 30, 2014 at 19:23
6 Answers
Reset to default 4I don't think there's any way to do this using standard HTML forms.
Take a look at 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. I think in IE you may need to use an 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.
Set it in web.config, in the httpRuntime section :
<httpRuntime executionTimeout="90" maxRequestLength="4096" /> <-- number of bytes
That should be inserted under <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?
Untested (probably won't work in 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
}
You can try swfupload, if Flash is an option for you.
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.
I've seen that being done with flash but I'm not a flash guy.
本文标签: aspnetAnyway to determine file size before uploadStack Overflow
版权声明:本文标题:asp.net - Anyway to determine file size before upload? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741501129a2382060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论