Solved

HTML INPUT tag events

Posted on 2009-05-18
4
871 Views
Last Modified: 2012-05-07
I am using the HTML INPUT tag on a website (type="File") to allow the users to upload files.   It looks like the default max file size is 4MB.   If the file is larger than that, it just clears the input control and does nothing everytime there is a postback.   Is there an event that fires off or some way that I can display a message to the end user letting them know that the file was too large?
0
Comment
Question by:amb1313
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Krummelz
ID: 24412803
You will need to create the uploader as follows:

<input ID="File" Type="File" RunAt="Server" causesvalidation="False" />

make sure the runat="server" is added so that you can work with it from the code.

then you can use the File.PostedFile.ContentLength to check if their file has exceeded the required size. ContentLength is the size in bytes.
0
 

Author Comment

by:amb1313
ID: 24413323
Does it matter that this is a 1.1 .NET application?   I tried adding the causesvalidation="False" to my control but it didn't seem to do anything.

Below is a snippet of what I am using.   The upload button is where I want to have all my validation take place.   If the file is over 4MB, when I click on the Upload button, the event never fires off.  Instead the page refreshes and the value for the input control is lost.   I know I can use a config setting to increase that 4MB limit but I don't really want to do that.   I want to enforce that limitation in addition to letting them know that a file size was too big if it's greater than that.


<input id="txtNewDocument" type="file" name="txtNewDocument" runat="server" onkeydown="return false;" causesvalidation="False">
 

<asp:button id="btnUploadDocument" Runat="server" Text="Upload Document"></asp:button>

Open in new window

0
 

Accepted Solution

by:
amb1313 earned 0 total points
ID: 24416369
Well I ended up finding  aolutiion to this.   I don't really think this is ideal but it will work for what I need.

What I did was add the line of code below to my web.config to allow file sizes of 10MB instead of the default of 4MB.   This allows my code-behind to fire off and then I'll do the check in there by looking at the PostedFile.ContentLength to see how large it is.   If it's more than 4MB, I'll display a message to the user and prevent the upload.

<httpRuntime maxRequestLength="10240" />
0
 
LVL 5

Expert Comment

by:Krummelz
ID: 24418905
Ok, thats what I did aswell -  just show them a message after posting back. If it was .NET 3.5 you could probably use it in an update panel and not lose the value in the upload box. Or save it in viewstate on postback, and restore the value from viewstate if the check fails. But then, if the file is too large, then you would probably expect the value to be lost, because the user will have to choose another file anyway.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now