Returning an Image File from a Web Service

The GetImage web service (Listing 11.2) returns a graphic image to a client. The service provides two methods. The first method, AvailableFiles, lists the files that are available in the server’s Images folder. The second method, GetFile, returns a requested file to the client:

string() AvailableFiles() byte() GetFile(ByVal string)

To create the GetImage web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. 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 GetImage. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. 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 11.2.

Listing 11.2 GetImage.asmx.vb

start example
Imports System.IO <WebMethod()> Public Function AvailableFiles() As String()     AvailableFiles = Directory.GetFiles("C:\Images") 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
end example

The AvailableFiles method uses the Directory class GetFiles method to retrieve a string array that contains the files from the C:\Images folder. The GetFile method opens the specified file and reads the file’s contents into a byte array, which it then returns to the caller.

Requesting a File from the Web Service

The C# program in Listing 11.3, RequestRemoteImage.cs, interacts with the GetImage web service to retrieve an image file. The program then stores the image file on disk. When you run the program, your screen will display a form similar to that shown in Figure 11.2, which you can use to list the names of the available graphics files and then to retrieve the file that you desire.


Figure 11.2: Requesting an image file from a web service

To create the RequestRemoteImage.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual C# Projects. Then, within the Templates field, click Windows Application. Finally, within the Location and Name fields, specify the folder within which you want to store the program and the program name RequestRemoteImage. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Using your mouse, drag and drop the list box and button previously shown in Figure 11.2 onto the page.

  4. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the code window, type the code in Listing 11.3.

Listing 11.3 RequestRemoteImage.cs

start example
using System.IO; private void Form1_Load(object sender, System.EventArgs e) {    localhost.Service1 WS = new localhost.Service1();    string [] FileList = WS.AvailableFiles();    listBox1.DataSource = FileList; } private void button1_Click(object sender, System.EventArgs e) {    byte [] ImageFile;    localhost.Service1 WS = new localhost.Service1();    string Filename = listBox1.Text;    if (Filename != "")         {         try           {          ImageFile = WS.GetFile(Filename);          FileStream outputFile;          try            {                 // Store the file in the current directory                 // So remove drive and path information                 Filename = Path.GetFileName(Filename);              outputFile = File.Open(Filename, FileMode.Create, Ä                FileAccess.Write);              outputFile.Write(ImageFile, 0, ImageFile.Length);              outputFile.Close();                         MessageBox.Show("File successfully copied");               }          catch (System.Exception ex)            {              MessageBox.Show("Error writing file" + ex.Message);            }           }         catch (System.Exception ex)           {           MessageBox.Show(ex.Message);           }    } }
end example

The program first retrieves the filename the user desires from the list box. Then, the program calls the web service’s GetFile method to retrieve a byte array that contains the file’s contents. The program then writes the contents of the byte array to a file on the client’s disk.

Displaying an Image File

The previous RequestRemoteImage program retrieved and stored an image file on disk. The C# program in Listing 11.4, DisplayRemoteImage.cs, uses the GetImage web service to retrieve a file. After the program gets the file’s contents, it displays the image within a PictureBox, as shown in Figure 11.3.

click to expand
Figure 11.3: Retrieving and displaying an image file

To create the DisplayRemoteImage.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual C# Projects. Then, within the Templates field, click Windows Application. Finally, within the Location and Name fields, specify the folder within which you want to store the program and the program name DisplayRemoteImage. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Using your mouse, drag and drop the list box, picture box, and buttons previously shown in Figure 11.3 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/GetImage/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. 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 11.4.

Listing 11.4 DisplayRemoteImage.cs

start example
using System.IO; private void button1_Click(object sender, System.EventArgs e) {    byte [] ImageFile;    localhost.Service1 WS = new localhost.Service1();    string Filename = listBox1.Text;    if (Filename != "")      {        try          {          ImageFile = WS.GetFile(Filename);          FileStream outputFile;          try             {             // Store the file in the current directory             // So remove drive and path information             Filename = Path.GetFileName(Filename);             outputFile = File.Open(Filename, FileMode.Create,             FileAccess.Write);             outputFile.Write(ImageFile, 0, ImageFile.Length);             outputFile.Close();                     pictureBox1.Image = Image.FromFile(Filename);           }          catch (System.Exception ex)           {              MessageBox.Show("Error writing file" + ex.Message);           }          }        catch (System.Exception ex)          {          MessageBox.Show(ex.Message);          }    } } private void Form1_Load(object sender, System.EventArgs e) {    localhost.Service1 WS = new localhost.Service1();    string [] FileList = WS.AvailableFiles();    listBox1.DataSource = FileList; }
end example

The program uses the web service to retrieve a byte array that contains the image file’s contents. The program then writes the array’s contents to a file on disk. Then, the program assigns the image file’s contents to a picture box control that displays the image on the screen.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

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