Upload With Ease
Let users send files to your servers without third-party components or workarounds.
October 30, 2009
UI Tips
LANGUAGES: HTML
ASP.NET VERSIONS:1.0 | 1.1
Upload With Ease
Let users send files to your servers without third-partycomponents or workarounds.
By Brad McCabe
One challenge of Web development is uploading files to theWeb server for processing and storage. ASP.NET now provides built-infile-upload support without any need for third-party components or workarounds.
The file-upload process is accessible via two differentAPIs - the Request.Files collection and the servercontrol. Let's take a look at using the server controlto upload a file.
Start a clean Web form and add this HTML to it:
runat="server" ID="Form1"> Select File To Upload: runat="server" NAME="UploadFile"> Text="Upload"> Now add one line to the onClick event of the commandbutton server control: UploadFile.PostedFile.SaveAs("c:upload.txt") When this page is run you willsee a basic page with two buttons (see Figure 1).
Figure 1. ASP.NET makes file uploadseasy. The file-upload control creates the browse button automatically, lettinga user navigate easily to a local file or path. When the user clicks on the Upload button, the file listedin the textbox is uploaded to the server and is, in this example, saved as afile named upload.txt in the root of the server's C drive. Of course, in areal-world application, you want to pass in a name to the SaveAs functioninstead 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 linesof code. Got a UI question, tip, or idea for a topic you'd like meto cover? I'd love to hear it. E-mail me at [email protected]. Brad McCabe is thetechnical evangelist for Infragistics. Brad also has been a systems architectand consultant for Verizon Communications and many other clients, and he was aleading .NET evangelist within Ajilon Consulting. His primary interests includeASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networkingtechnologies. E-mail him at [email protected].
About the Author
You May Also Like