You can introduce a similar upload strategy through a dedicated XML Web service and Windows client. The XML Web service plays the same role as the ASP.NET Web page: It accepts a buffer of bytes and writes them to the OrderProcessor directory. At this point, the OrderProcessor takes over. Listing 16-16 shows the XML Web service. Listing 16-16 The Excel upload XML Web servicePublic Class OrderService Inherits System.Web.Services.WebService <WebMethod()> _ Public Sub UploadExcelFile(ByVal filename As String, _ ByVal bytes As Byte()) ' Determine the location and name for the new file. Dim SavePath As String SavePath = ConfigurationSettings.AppSettings("MonitorPath") SavePath &= "\" & filename ' Create a FileStream. Dim fs As New FileStream(SavePath, FileMode.CreateNew) ' Write the received bytes to the file. fs.Write(bytes, 0, bytes.Length) fs.Close() End Sub End Class The Windows client provides the functionality for selecting a file and transmitting it to the XML Web service (as shown in Figure 16-10). Figure 16-10. The Windows upload client
The code shown in Listing 16-17 is similar to the ASP.NET upload page, although the file selection is provided through the OpenFileDialog class. Listing 16-17 The Windows client form codePublic Class WindowsClient Inherits System.Windows.Forms.Form Friend WithEvents cmdBrowse As Button Friend WithEvents cmdUpload As Button Friend WithEvents Label1 As Label Friend WithEvents lblInfo As Label Friend WithEvents dlgOpenFile As OpenFileDialog Friend WithEvents txtFile As TextBox Private Sub cmdBrowse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdBrowse.Click dlgOpenFile.ShowDialog() txtFile.Text = dlgOpenFile.FileName End Sub 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 txtFile.Text = "" Then lblInfo.Text = "No file specified." ElseIf Not txtFile.Text.EndsWith(".xls") Then lblInfo.Text = "You can only upload Excel files." Else Dim fs As FileStream Try ' Create the byte array. fs = New FileStream(txtFile.Text, FileMode.Open) Dim Bytes As Byte() ReDim Bytes(fs.Length) fs.Read(Bytes, 0, fs.Length) ' Submit the byte array to the XML Web service. Dim Proxy As New localhost.OrderService() Dim FileName As String = Path.GetFileName(txtFile.Text) Proxy.UploadExcelFile(FileName, Bytes) ' Display the confirmation message. lblInfo.Text = FileName & " uploaded successfully." txtFile.Text = "" Catch Err As System.Web.Services.Protocols.SoapException lblInfo.Text = "Server error: " & Err.Message Catch Err As Exception lblInfo.Text = Err.Message Finally If Not (fs Is Nothing) Then fs.Close() End If End Try End If End Sub End Class |