views:

105

answers:

6

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?

A: 

You can try swfupload, if Flash is an option for you.

thomask
+3  A: 

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

David Neale
A: 

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.

Angelus
+2  A: 

Hi Scott,

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
Marko
Actually have that in the web.config already, however when we go to submit a file it won't submit if larger than our limit, however it will also not let the user know it didn't, or throw any kind of an error because we're not sure where/how to catch it and display something.
Scott
You can check when the users click submit, see my 2nd code snippet.
Marko
Have you tried implementing a custom error page? Re may be able catch the error there.
David Murdoch
Whoever marked the answer down, I'd love to hear why.
Marko
Marko, isn't that impossible to run unless an object in memory of the file has been created? Due to the web.config size constraint, this will never happen. At least that's the way ours is setup.
Scott
+1 to bring it back to 0. :-)
David Murdoch
And David, can you further elaborate on your idea?
Scott
A: 

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
}
David Murdoch
Can't use ActiveX =/
Scott
A: 

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 to 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>
Brian
I'm not sure if this will work. Main thing is, where will this exactly run checkSize(). I don't see how it can find info about this file if it doesn't have access to the users file system, and without uploading it first.
Scott
I tested it before posting and it does work. It runs "checkSize" in the onClick handler of the submit button, which is executed before the submit happens (returning false in an onClick event cancels the submit). Note that it only works in Firefox and Chrome, as I mentioned in the post.
Brian