The Web Front End

The next step is to remove the requirement for the sales manager to download and copy the Excel files. The easiest approach that is guaranteed to be supported for all sales notebooks is to create a basic ASP.NET upload page.

ASP.NET allows uploads through the HtmlInputFile control. To allow a user-initiated upload, you need to take three steps:

  1. Set the encoding type of the form to "multipart/form-data". You can do this by setting a property of the HtmlForm control class, or you can just find the tag in your ASPX file and modify as shown here. If you don't make this change, your uploading code won't work.

     <form  enctype="multipart/form-data" runat="server">   <!  Server controls go here, including the HtmlInputFile control.  > </form> 
  2. Add the HtmlInputFile control, either by hand as an <input type="file" runat="server"> tag or by using Visual Studio .NET, in which case you must right-click on the HTML control and choose Run As Server Control.

  3. Add a button that triggers the upload and saves the file to the server's hard drive. (The Browse button functionality is hard-wired into the browser.) You can use the HtmlInputFile.PostedFile.SaveAs method.

Figure 16-9 shows the simple ASP.NET page used by the Graphic Design Institute.

Figure 16-9. The ASP.NET upload page

graphics/f16dp09.jpg

The class code for the page is shown in Listing 16-15. The code reacts when the Upload button is clicked. It checks whether the file is valid (and not suspiciously large) and then transfers it to the location specified in the MonitorPath setting in the web.config file. The OrderProcessor then takes over the rest of the work.

Listing 16-15 The ASP.NET upload page code
 Public Class Upload Page     Inherits System.Web.UI.Page     Protected WithEvents FileUpload As HtmlInputFile     Protected WithEvents FileInput As HtmlInputFile     Protected WithEvents lblInfo As Label     Protected WithEvents cmdUpload As Button     Private Sub cmdUpload_Click(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles cmdUpload.Click         ' Check that a file is actually being submitted.         If FileInput.PostedFile Is Nothing Then             lblInfo.Text = "No file specified."         Else             If Not FileInput.PostedFile.FileName.EndsWith(".xls") Then                 lblInfo.Text = "You can only upload Excel files."             ElseIf FileInput.PostedFile.ContentLength > 20000 Then                 lblInfo.Text = "The file is too large to be permitted."             Else                 Try                     ' The saved file will retain its original file                     ' name, but be stored in the ExcelFiles directory.                     Dim SavePath As String                     SavePath = _                       ConfigurationSettings.AppSettings("MonitorPath")                     SavePath &= "\" & Path.GetFileName( _                       FileInput.PostedFile.FileName)                     ' Save the file.                     FileInput.PostedFile.SaveAs("E:\ExcelFiles\" & _                                                 ServerFileName) 
                     lblInfo.Text = "File " & SavePath                     lblInfo.Text &= " uploaded successfully."                 Catch Err As Exception                     lblInfo.Text = Err.Message                 End Try             End If         End If     End Sub End Class 

Alternatively, the ASP.NET page could perform the processing of the order file itself, by interacting directly with the component. However, we didn't choose this approach for a few reasons:

  • While transitioning to this new solution, it's logical to expect that some users will continue to e-mail the orders instead of uploading them. Using the OrderProcessor component simplifies life by ensuring that all files are processed in exactly the same manner.

  • By using the OrderProcessor component, you gain the flexibility to process files in batches or when a computer is available. If the ASP.NET page were to take over this responsibility, the user would need to wait while the file was being processed before receiving a response. This could also lead to problems if the user assumed there was a problem and refreshed the page, which would send the same file multiple times.



Microsoft. NET Distributed Applications(c) Integrating XML Web Services and. NET Remoting
MicrosoftВ® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting (Pro-Developer)
ISBN: 0735619336
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net