UI Tips
LANGUAGES: HTML
ASP.NET VERSIONS: 1.0 | 1.1
Upload With Ease
Let users send files to your servers without third-party components or workarounds.
By Brad McCabe
One challenge of Web development is uploading files to the Web server for processing and storage. ASP.NET now provides built-in file-upload support without any need for third-party components or workarounds.
The file-upload process is accessible via two different APIs - the Request.Files collection and the server control. Let's take a look at using the server control to upload a file.
Start a clean Web form and add this HTML to it:
Now add one line to the onClick event of the command button server control:
UploadFile.PostedFile.SaveAs("c:\upload.txt")
When this page is run you will see a basic page with two buttons (see Figure 1).
Figure 1. ASP.NET makes file uploads
easy. The file-upload control creates the browse button automatically, letting
a user navigate easily to a local file or path.
When the user clicks on the Upload button, the file listed in the textbox is uploaded to the server and is, in this example, saved as a file named upload.txt in the root of the server's C drive. Of course, in a real-world application, you want to pass in a name to the SaveAs function instead of using a hard-coded name and file extension.
Thanks to ASP.NET's built-in support for file uploading, you now can use this functionality in your applications with a few simple lines of code.
Got a UI question, tip, or idea for a topic you'd like me to cover? I'd love to hear it. E-mail me at [email protected].
Brad McCabe is the technical evangelist for Infragistics. Brad also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist within Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at [email protected].