
The example ASP.NET C# project shows you how to upload images to a web server using the FileUpload class. It’s common for web applications to allow users of the site to upload files, photos, images, and pictures of various file types to a web site. The provided C Sharp solution will show you how to accomplish the task without using a database.
The FileUpload class can be found in the System.Web.UI.WebComtrols namespace. It displays a browse button that enables users to select a file to upload to the web server. The user can select the file by clicking the Browse button, then locating it in the Choose File dialog box. The FileUpload control does not automatically save the file to the server upon selection. You must implement logic in the code-behind page to allow the user to submit the specified file. The provided example includes a Button control that, when clicked, calls an event in the code-behind page to upload the image to the server. So the work has been done for you. In addition, a DataList control is used to display all uploaded images that are currently present in the images folder on the server.
Another thing to note about the FileUpload control is that it limits the size of the files that can be uploaded. This is done to guard against denial of service attacks against the web server. The default size limit is 4096 kilobytes (KB), or 4 megabytes (MB). However, this limit can be increased by updating the Web.config file. The example project will not allow users to upload a file that’s greater than 2MB in size. However, you can change this to fit your needs by updating the logic in the Default.aspx.cs code-behind page.
Another feature built-in to the provided ASP.NET C# solution is that users can only upload images with a file type of GIF, PNG, JPG, and JPEG. If the user attempts to upload a file with a different file type, the Default.aspx will display a custom error message. Also, if the user attempts to upload an image where the file name already exist on the server, an error message will be display. Therefore, duplicate file names are not allowed. Again, this is custom code that’s implemented in the code-behind page.
The provided demo project also includes the ability for users to delete files from the server. Most ASP.NET examples found on the Internet for managing photos and pictures do not include this logic. However, in the real world, this is a highly desirable feature.
You can get the full ASP.NET C# source code for this project by selecting the download link below.