O'Reilly logo

ASP.NET Cookbook by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.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 15.2 but 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 15.2 and then, if you like, add a control that will show the results of the processing—for example, a DataGrid 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 length of the filename and the file contents are both greater than 0—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 DataGrid control.

Example 15-8 through Example 15-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 15.2s example output and is shown in Figure 15-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 Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required