File Size

  • Thread starter Thread starter rn5a
  • Start date Start date
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"?
 
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
 
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
 
Back
Top