Downloading a file from a server is a common requirement of web applications, whereas uploading a file is less so. For example, you might want your application to allow users to download PDF, binary, or image files to their browsers. Alternatively, you might have a content management system to which you want to let users upload images or binary files.
ASP.NET provides an easy-to-use and flexible infrastructure for completing either task. You can download a file to the browser for display, storage, or printing by streaming it to the Response object, a simple task in ASP.NET.
Uploading a file to the web server for storage is straightforward, particularly with the FileUpload control introduced with ASP.NET 2.0.
Sometimes you may want to upload a file to the server for processing only, without storing it there, and at other times you may need to store the contents of an uploaded file to a database. For example, you might want to do the former to avoid having to deal with problems associated with files being uploaded with the same names, inadvertently filling the hard drive, or allowing the ASP.NET write privileges on the local filesystem. Storing an uploaded file in a database is useful when you want to keep a complete record of the file set apart from the web server's files system. The recipes in this chapter show you how to do all of these things.