Uploading a Binary File to a Web Service

The Visual Basic .NET program in Listing 11.8, UploadBinaryFile.vb, interacts with the FileUpload web service to let users upload files to a remote server. When you run the program, your screen will display a form similar to that shown in Figure 11.6, which you can use to select a file that you want to upload.


Figure 11.6: Uploading a file to a web service

To create the UploadBinaryFile.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 Basic 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 UploadBinaryFile. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s components.

  3. Using your mouse, drag and drop the text boxes and buttons previously shown in Figure 11.6 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/FileUpload.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.8.

Listing 11.8 UploadBinaryFile.vb

start example
Imports System.IO Private Sub Button1_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button1.Click   Dim InputFile As FileStream   Dim FileLength As Long   If TextBox1.Text.Length > 0 Then     Try         TextBox2.Text = ""         InputFile = File.Open(TextBox1.Text, FileMode.Open, _ Ä        FileAccess.Read)         FileLength = InputFile.Length         Dim DataBuffer(FileLength - 1) As Byte         InputFile.Read(DataBuffer, 0, FileLength)         InputFile.Close()         Dim WS As New localhost.Service1()         If WS.FileUpload(DataBuffer, Path.GetFileName(TextBox1.Text)) _ Ä          =  1 Then           TextBox2.Text = "File upload successful"         Else           TextBox2.Text = "Error uploading file"         End If       Catch Ex As Exception         Throw New Exception("Unable to open specified file")       End Try     End If End Sub
end example

After the user clicks the file upload button, the code checks to ensure the user has specified a filename. The code then opens the file and reads the file’s contents into a byte array. Then, the code calls the FileUpload web service’s FileUpload method to store the file’s contents on the remote server.




. 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