Providing Multimedia Files Using a Web Service

As it turns out, the process of retrieving an image, video, or audio file from a web service is an identical process. The GetMedia web service (Listing 11.5) returns the various media types from the server’s Media folder. (You will need to create the folder on your system and place some sample files into the folder.) To run the web service you may need to change file permissions on your server to allow the service to access the Media folder. The web service provides the following methods:

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

To create the GetMedia 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 GetMedia. 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.5.

Listing 11.5 GetMedia.asmx.vb

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

As you can see, the GetMedia web service is similar to the GetImage service previously discussed in the chapter. The AvailableFiles method again returns an array of strings that correspond to the names of files within the server’s Media folder. After the client selects a file and calls the GetFile method, the web service reads the file’s contents into a byte array that it returns to the caller.

Retrieving and Using Multimedia Data

The ASP.NET page in Listing 11.6, RemoteMediaPlayer.aspx, interacts with the GetMedia web service to return the media file you select. Using the form, you can display the names of the multimedia files that reside in the server’s folder. When you run the program, your screen will display a form similar to that shown in Figure 11.4.

click to expand
Figure 11.4: Retrieving multimedia files from the server’s Media folder

After the page retrieves an image file, it will display the file’s contents. If you retrieve an audio or video file, the program will create a link you can use to launch the file within the Windows Media Player as shown in Figure 11.5.

click to expand
Figure 11.5: Displaying and playing multimedia files

To create the RemoteMediaPlayer ASP.NET page, 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 Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name RemoteMediaPlayer. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, button and text boxes, list box, hyperlink, and image).

  3. Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 11.5 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/GetMedia/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.6.

Listing 11.6 RemoteMediaPlayer.aspx.vb

start example
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()    Dim Filename As String = ListBox1.SelectedItem.ToString()    If (Filename <> "") Then      Try          ImageFile = WS.GetFile(Filename)          Dim OutputFile As FileStream          Try            ' Store the file in the current directory            ' So remove drive and path information            Filename = "C:\TEMP\" + Path.GetFileName(Filename)            OutputFile = File.Open(Filename, FileMode.Create, _                 FileAccess.Write)            OutputFile.Write(ImageFile, 0, ImageFile.Length)            OutputFile.Close()            Dim Extension As String = _ Ä          Path.GetExtension(Filename).ToString().ToUpper()            If ((Extension = ".JPG") Or (Extension = ".TIF")) Then              Image1.Visible = True              Image1.ImageUrl = Filename            End If            If ((Extension = ".WAV") Or (Extension = ".AVI")) Then              HyperLink1.NavigateUrl = "file://" + Filename              HyperLink1.Text = HyperLink1.NavigateUrl              HyperLink1.Visible = True            End If          Catch Ex As Exception            TextBox1.Text = "Error writing file " + Ex.Message          End Try        Catch Ex As Exception          TextBox1.Text = ex.Message        End Try      End If End Sub
end example

When the page downloads a file, it places the file copy into the Temp folder. To run this ASP.NET you may need to change file permissions on your server to allow the service to access the Temp folder. (If your drive does not have a Temp folder, you will need to create one.)

If the program downloads an image file (in this case a JPEG or TIFF file), the program displays the file’s contents using an image control. If the program downloads an audio or video file, the program creates a hyperlink to the file using a hyperlink label control. When the user clicks on the link, the program will launch the Windows Media Player to play the file’s contents.




. 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