18.2. Uploading a File to the Web Server
You need to provide the ability for a user to upload a file to the web server filesystem.
Add to your page a
FileUpload control and a button to initiate the upload process. When the user clicks the button, the code-behind can save the file to the filesystem.
Create a folder within your application file structure where the uploaded files will be placed, such as as one named uploads.
In the .aspx file:
Add an Upload (or equivalently named) button.
In the code-behind class for the page, use the .NET language of your choice to:
Verify, in the Upload button click event handler, that the file content has been uploaded by checking the
HasFileproperty of the
Save the file to the local filesystem on the server using the
SaveAsmethod of the
Example 18-4 shows the .aspx file for an application we’ve written to demonstrate this solution by allowing you to browse for a file and uploading the chosen file to your web server’s local filesystem when you click the Upload button. The code-behind files for the application are shown in Examples 18-5 (VB) and 18-6 (C#). The UI for uploading a file is shown in Figure 18-3.
Figure 18-3. UI for uploading a file
Most applications do not require uploading files to a web server. Nevertheless, here are a few examples of applications for which ...