18.3. Processing an Uploaded File Without Storing It on the Filesystem

Problem

You want a user to be able to upload a file to the web server for immediate processing without having to first store the file.

Solution

Implement the solution described in Recipe 18.2. Instead of writing the file to the filesystem, use the input stream containing the uploaded file to process the data.

For the .aspx file, follow the steps for implementing the .aspx file in Recipe 18.2 and then, if you like, add a control that will show the results of the processing—for example, a GridView control to display the contents of an uploaded XML file.

In the code-behind file for the page, use the .NET language of your choice to:

  1. Verify, in the Upload button click event handler, that the file has been uploaded—that is, that the HasFile property of the FileUpload control is set to True—and (if appropriate) that it is a valid XML file.

  2. Load the updated data and, if you elected to include a control for showing the contents of the uploaded file, bind the uploaded data to the control—for example, a GridView control.

Examples 18-8, 18-9 through 18-10 show the .aspx file and VB and C# code-behind files for an application we’ve written to illustrate this solution. The initial output is identical to Recipe 18.2’s example output and is shown in Figure 18-3.

Discussion

This recipe demonstrates the concept of uploading files and processing them without having to store them to the local filesystem, as you might do with the contents ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.