Published: 23/04/2002 14:43:09 Posted by: Robert Chartier Posted by: Rob@aspfree.com This article explains how to create and use a WEB service transmitted by binary data. This is quite easy.
============================================================================================================================================================================================================= ================
In the example, the image data will be taken out from the local disk, and then the image is transmitted to the caller using SOAP information. Now discuss the relevant web services, and pay special attention to the data encoding.
0. [WebMethod (description = "get an image sale base64 encoding")] 1. Public Byte [] getimage () {2. Return getBinaryFile ("c: // inetpub // wwwroot // Webservices // public //iMages //logo.gif "); 3.} 4.
Note that the function returns byte [] (byte array). .NET will automatically encode the returned data into base64 format.
The following "getBinaryFile () function reads the file from the hard disk, then converts the image file data to Byte []:
0. Public Byte [] getBinaryFile (String FileName) {1. IF (file.exists (filename)) {2. Try {3. FileStream S = file.openread (filename); 4. Return ConvertStreamTobyTeBuffer (s); 5. } CatCH (Exception E) {6. Return New Byte [0]; 7.} 8.} else {9. Return New Byte [0]; 10.} 11.} 12. 13. Public Byte [] ConvertStreamTobytebuffer (SystemTemTeTebuffer {14. Int B1; 15. System.io.MemoryStream TempStream = new system.io.MemoryStream (); 16. While ((b1 = theretream.readbyte ())! = - 1) {17 TempStream.writebyTe ((Byte) B1)); 18.} 19. Return TempStream.toArray (); 20.
The client program can now be written on .NET, the process is as follows: 1. Creating a C # Application Form 2. Add a Picture Box control, named PCT13. Add a web reference to: http: // rob. Santra.com/webservices/public/images/index.asmx?wsdl4. Write the following code in the program's event driver (form1_load, or other event): 0. CoM.Santra.Rob.Images images = new com.santra. Rob.Images (); 1. Byte [] image = images.getimage (); 2. System.io.MemoryStream MemStream = new system.io.MemoryStream (image); 3. Bitmap BM = New Bitmap (Memstream); 4 PCT1.Image = BM; 5. 6. If you create this service on your own server, you should change the Web Reference address, while com.santra.rob.images () objects should also increase Web Reference The object created when creating is consistent.
In the above code, the customer applies for an array containing image data to the server, and converts the array to the memorystream, then loaded into the Bitmap object, and finally displays the resulting image with the PCT1 image control.
The whole process is so simple!
The following is all code:
0. <% @ Webservice Language = "C #" class = "images"%> 1. 2. Using system; 3. using system.web.services; 4. using system.io; 5. 6. [Webservice (namespace = "http://rob.santra.com/webservices/public/images/", description = "Demonstration of use Base64 Encoding, in a web service using the .net framework.")]