This tutorial will demonstrate how to apply client side and server side filters on the file upload control using ASP.NET 4.0 and C#.
Adding the Default.aspx Page
To demonstrate how we can add client and server side validation to restrict file extensions to the file upload control, we will need a simple form. We will be using a file upload control, custom validator, and a button to cause post backs. At this point, I have created a new ASP.NET Empty Web Site. To begin:
Adding Client Side Validation
Because we cannot limit the file types that are viewed in the file browser, we must go about some different methods to inform our user what the file upload control is expecting. In this example, we will create some validation to allow only '.jpg' and '.jpeg' file types. To do this client side, we must implement some simple javascript and associate it with our file upload control. To add in the javascript portion, open up Default.aspx and add the following script block into the <head> tag:
Let's go over what this function actually does. First, it gets the extension from the currently selected file and then checks to see if it is valid. If it finds an invalid extension, it executes an alert popup that displays a message saying that the current file type is not allowed. Next, we need to create the association between our javascript function and file upload control. To do this, open Default.aspx.cs up for editing and add in the following code to the Page_Load event method:
I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Adding Server Side Validation
Next, we will add in some code that will cause validation on the server so that we can prevent the user from uploading invalid file types. To begin, open up Default.aspx to design mode and double click the customvalidator to generate the CustomValidator1_ServerValidate event method, then add the following code to that method:
Let's go over what this code is doing. First, we setup the valid file extensions that we will be looking for. Then, we check the current file extension and see if it matches any of our valid extensions. If it does we validate it, if not the validation fails and the error message is displayed in our custom validator.
We used over 10 web hosting companies before we found Server Intellect. Our cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Testing
To test this out:
To demonstrate how we can add client and server side validation to restrict file extensions to the file upload control, we will need a simple form. We will be using a file upload control, custom validator, and a button to cause post backs. At this point, I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a fileupload control onto the web form.
- Drag and drop a customvalidator next to the fileupload control.
- Change the ErrorMessage property to 'Please select a valid file type (*.jpg, *.jpeg).'
- Add a break line after the customvalidator.
- Add a button underneath the fileupload control.
Adding Client Side Validation
Because we cannot limit the file types that are viewed in the file browser, we must go about some different methods to inform our user what the file upload control is expecting. In this example, we will create some validation to allow only '.jpg' and '.jpeg' file types. To do this client side, we must implement some simple javascript and associate it with our file upload control. To add in the javascript portion, open up Default.aspx and add the following script block into the <head> tag:
<script type="text/javascript"> |
Let's go over what this function actually does. First, it gets the extension from the currently selected file and then checks to see if it is valid. If it finds an invalid extension, it executes an alert popup that displays a message saying that the current file type is not allowed. Next, we need to create the association between our javascript function and file upload control. To do this, open Default.aspx.cs up for editing and add in the following code to the Page_Load event method:
protected void Page_Load(object sender, EventArgs e) |
I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Adding Server Side Validation
Next, we will add in some code that will cause validation on the server so that we can prevent the user from uploading invalid file types. To begin, open up Default.aspx to design mode and double click the customvalidator to generate the CustomValidator1_ServerValidate event method, then add the following code to that method:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) |
Let's go over what this code is doing. First, we setup the valid file extensions that we will be looking for. Then, we check the current file extension and see if it matches any of our valid extensions. If it does we validate it, if not the validation fails and the error message is displayed in our custom validator.
We used over 10 web hosting companies before we found Server Intellect. Our cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Testing
To test this out:
- Load up the web site.
- Click browse.
- Select an invalid file type.
- Click open.
- Notice the popup that alerted you the file type was invalid.
- Click the button.
- Notice the error message from the custom validator being displayed.
- Click browse.
- Select a valid file type.
- Click open.
- Notice that there is no popup.
- Click the button.
- Notice there is no error message from the custom validator.
No comments:
Post a Comment