18.4. Storing the Contents of an Uploaded File in a Database
You need to provide the ability for a user to upload a file to the web server that will be processed later, so you want to store the file in the database.
Implement the solution described in Recipe 18.2. When the user clicks a button to initiate the upload process, instead of writing the file to the filesystem, use the input stream containing the uploaded file along with ADO.NET to write the file to a database.
For the .aspx file, follow the steps for implementing the .aspx file in Recipe 18.2.
In the code-behind class for the page, use the .NET language of your choice to:
Process the Upload button click event and verify that a file has been uploaded.
Open a connection to the database.
Build the command used to add the data to the database and insert the file data.
The application we’ve written to demonstrate this solution uses the same .aspx file as Recipe 18.2’s example (see Example 18-4). The code-behind for our application is shown in Examples 18-11 (VB) and 18-12 (C#). The initial output is the same as Recipe 18.2’s example output and is shown in Example 18-3.
Storing an uploaded file in a database is useful when a complete, unmodified upload record is required to be set apart from the web server’s filesystem, when the file contains sensitive information, or when additional metadata needs to be stored with the file. It is common to store the uploaded data in a database and then process the data immediately ...