File Size

R

rn5a

I am giving users the option to upload files from their hard disk to
the server but only those files will be uploaded whose size is less
tha or equal to 1 MB.

I am using the ASP.NET FileUpload control to design the interface.
This is the code:

<script runat="server">
Sub Page_Load(.....)
If (Page.IsPostBack) Then
If (fudFile1.FileName <> "") Then
If (fudFile1.FileBytes.Length / 1048576 > 1) Then
lblMessage.Text = "File size cannot exceed 1 MB"
Else
fudFile1.SaveAs(Server.MapPath(fudFile1.FileName))
lblMessage.Text = "File uploaded"
End If
End If
End If
End Sub
</script>

<form runat="server">
<asp:FileUpload ID="fudFile1" runat="server"/><br><br>
<asp:Button ID="btnUpload" Text="UPLOAD FILE" runat="server"/>
<asp:Label ID="lblMessage" runat="server"/>
</form>

Now what I find is if I try to upload a file whose size is greater
than 1 MB but less than or equal to 4 MB, then, as expected, the Label
renders the message "File size cannot exceed 1 MB" but if the file
size exceeds 4 MB, then the Label doesn't render the "File size cannot
exceed 1 MB" message.

Instead, IE just renders the omnipresent "The page cannot be
displayed" page with the error "Cannot find server or DNS Error" at
the very end of the page.

Why isn't the Label displaying the "File size cannot exceed 1 MB"
message when the file size is greater than 1 MB but less than or equal
to 4 MB? How do I ensure that even if the file size exceeds 4 MB,
users are still shown the Label with the message "File size cannot
exceed 1 MB"?
 
E

Eliyahu Goldin

You get the file size on server side, that is after the file has already
been uploaded. Files larger than 4M fail to upload and this causes your IE
error. I am not aware of any simple ways of evaluating file size on client
side.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
M

Mark Rae

It can be done with Javascript, but only if the browser local security is
low enough to allow it. Its probably OK for intranets, but not for an
internet scenario.

This example is quite comprehensive but its a bit old now. Should still
be a good pointer.

http://www.faqts.com/knowledge_base/view.phtml/aid/1685

Well, yes, but that's using an ActiveX object - specifically
FileSystemObject - so no use for anything other than IE...
 
J

John Timney \(MVP\)

If I recall it, it works in both IE and Netscape et al as it taps into Java
objects for non IE browsers but it has to be ran as a trusted script. Hence
my caveat that it was no good for internet. I'd only ever look to use
something as quirky as this in an extreme case.

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top