|
|
In Chapter 11, “Integrating Binary Data into .NET Web Services,” you learned how to retrieve image, audio, and video files from a web service. For a web service to return a file, the service’s code reads the file’s contents into a byte array, which the service then returns to the caller:
Dim InputFile As FileStream Dim FileLength As Long InputFile = File.Open(Source, FileMode.Open, FileAccess.Read) FileLength = InputFile.Length Dim DataBuffer(FileLength - 1) As Byte InputFile.Read(DataBuffer, 0, FileLength) InputFile.Close() FunctionName = DataBuffer
The OfficeDocument web service in Listing 13.1 lets you retrieve a Word or Excel document from a server’s Documents folder (you must create the folder). Before you run the web service, you may need to set file permissions on your server to allow access to the Documents folder. Later in this chapter, you will learn ways you can retrieve files from your disk using a Windows service without having to change permissions for each folder you must access.
To create the OfficeDocument web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name OfficeDocument. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.1.
Listing 13.1 OfficeDocument.asmx.vb
Imports System.IO <WebMethod()> Public Function AvailableFiles() As String() AvailableFiles = Directory.GetFiles("C:\Documents") End Function <WebMethod()> Public Function GetFile(ByVal Source As String) As Byte() Dim InputFile As FileStream Dim FileLength As Long Try InputFile = File.Open(Source, FileMode.Open, FileAccess.Read) FileLength = InputFile.Length Dim DataBuffer(FileLength - 1) As Byte InputFile.Read(DataBuffer, 0, FileLength) InputFile.Close() GetFile = DataBuffer Catch Ex As Exception Throw New Exception("Unable to open specified file") End Try End Function
The Visual Basic .NET program in Listing 13.2, GetOfficeFile.vb, interacts with the OfficeDocument web service to retrieve a Word or Excel file from a remote server. When you run the program, your screen will display a form similar to that shown in Figure 13.1. If you click the List Files button, the program will prompt you to enter a password. For simplicity, this program uses the password Password. If you create a program for real-world use, you should integrate the authentication procedures discussed in Chapter 8, “Authenticating Users within .NET Services.” Next, the program will display a list of the files that the server’s Documents folder contains. After you select a file and click the Get File button, the program will download the program into the current directory.
Figure 13.1: Downloading Word or Excel files from a remote server
To create the GetOfficeFile.vb program, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and within the Name field the program name GetOfficeFile. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).
Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 13.1 onto the page.
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type localhost/OfficeDocument/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.2.
Listing 13.2 GetOfficeFile.vb
Imports System.IO Private Sub Page_Load(ByVal sender As System.Object, _ ÄByVal e As System.EventArgs) Handles MyBase.Load Try Dim WS As New localhost.Service1() Dim Filelist As String() Filelist = WS.AvailableFiles() Dim Str As String If (ListBox1.Items.Count = 0) Then For Each Str In Filelist ListBox1.Items.Add(Str) Next End If Catch Ex As Exception TextBox1.Text = Ex.Message End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ÄByVal e As System.EventArgs) Handles Button1.Click Dim ImageFile As Byte() Dim WS As New localhost.Service1() If (ListBox1.SelectedItem <> Nothing) Then Dim Filename As String = ListBox1.SelectedItem.ToString() Try ImageFile = WS.GetFile(Filename) Dim OutputFile As FileStream Try Filename = "C:\TEMP\" + Path.GetFileName(Filename) OutputFile = File.Open(Filename, FileMode.Create, _ ÄFileAccess.Write) OutputFile.Write(ImageFile, 0, ImageFile.Length) OutputFile.Close() TextBox1.Text = "File stored to " & Filename Catch Ex As Exception TextBox1.Text = "Error writing file " + Ex.Message End Try Catch Ex As Exception TextBox1.Text = ex.Message End Try Else TextBox1.Text = "You must select a file" End If End Sub
When a user runs the program, the Page_Load function will call the web service to retrieve the names of files available in the server’s Documents folder. The function will then assign the filenames to the Listbox control. After the user selects a file and clicks the Get File button, the program will call the web service to retrieve the corresponding file. In this case, the program stores the file within the Temp folder on the client’s PC.
|
|