How to use ASP.NET Web services and Visual C # .NET to send and receive binary documents
Summary WEB Services 1. On the File menu in Microsoft Visual Studio .NET, click New, and then click the item. 2. In the Visual C # project, select the ASP.NET Web service. Type or paste http: // localhost / docutp: // localhost / documentManagementService for your location, and then click OK. By default, service1.asmx is created and displayed in the design view. 3. On the View menu, click the code to display the code view of Service1.asmx. 4. Add the following WebMethods code to the Service1 class: [WebMethod] public bool SaveDocument (Byte [] docbinaryarray, string docname) {string strdocPath; strdocPath = "C: // DocumentDirectory //" docname; FileStream objfilestream = new FileStream ( Strdocpath, FileMode.create, FileAccess.Readwrite; ObjfileStream.write (DocbinaryArray, 0, DocbinaryArray.Length); ObjfileStream.close ();
Return True;}
[WebMethod] public int getDocumentlen (String DocumentName) {string strdocpath; strDocpath = "c: // DocumentDirectory //" DocumentName
FileStream Objfilestream = new filestream (strDocpath, filemode.open, fileaccess.read); int Len = (int) objfilestream.Length; objfilestream.close ();
Return Len;}
[WebMethod] public byte [] getDocument (string documentname) {string strdocpath; strdocpath = "c: // DocumentDirectory //" DocumentName;
FileStream objfilestream = new FileStream (strdocPath, FileMode.Open, FileAccess.Read); int len = (int) objfilestream.Length; Byte [] documentcontents = new [len] Byte; objfilestream.Read (documentcontents, 0, len); objfilestream .Close ();
Return DocumentContents;
Note: The above code saves documents to the
5. Add the following namespace to the beginning of Service1.asmx: use system.io; 6. Test Web Services: a. On debug menu, click Start to start the web service. This will start the web browser and display the Help page containing the service instructions. b. Make sure the SaveDocument, GetDocument, and GetDocumentlen methods are displayed. c. Turn off the web browser window to stop debugging. Back to top Set up clients for web services 1. On the File menu in Visual Studio .NET, click Add Item, and then click New Project. 2. In the Visual C # item list, select Windows Applications, and then click OK. FORM1 is created by default. 3. Add a web reference to the web service, as shown below: a. In the Solution Explorer, right-click the client project item. Then select Add Web references on the context menu. b. In the Add Web Reference dialog box, type the URL of the Web Services Description Language (WSDL) file pointing to the web service, then press ENTER. Note: The default location of the WSDL file is http://localhost/documentManagementService/service1.asmx? WSDL. c. In the Add Web Reference dialog box, click Add Reference. 4. Add two buttons to FORM1. Set the text property of Button1 to "Store Documents on the server". Set the BUTTON2's text property to "Retrieve Document from Server." 5. Double-click Button1 and Button2 to create a default Click event handler for the button. 6. Replace these processing programs with the following code: string sfile = "
private void button1_Click (object sender, System.EventArgs e) {FileStream objfilestream = new FileStream (sFile, FileMode.Open, FileAccess.Read); int len = (int) objfilestream.Length; Byte [] mybytearray = new Byte [len] ; objfilestream.Read (mybytearray, 0, len); localhost.Service1 myservice = new localhost.Service1 (); myservice.SaveDocument (mybytearray, sFile.Remove (0, sFile.LastIndexOf ( "//") 1)); Objfilestream.close ();
private void button2_Click {MemoryStream objstreaminput = new MemoryStream (object sender, System.EventArgs e) (); "." FileStream objfilestream = new FileStream (sFile.Insert (sFile.LastIndexOf (), "2"), FileMode.Create, FileAccess .Readwrite;
Localhost.service1 myservice = new localhost.service1 (); int Len = (int) myService.getdocumentlen (sfile.Remove (0, sfile.lastindexof ("//") 1)); byte [] mybyteaRray = new byte [ Len]; mybyteaRray = myservice.getdocument (sfile.Remove (0, sfile.lastIndexof ("//") 1); ObjFileStream.write (MyByteArray, 0, len); objfilestream.close ();} Note: sfile Variables must contain local file paths to upload to the document of the server. The document will be placed in the same folder after downloading and attached to the file name. 7. Add the following namespace to the beginning of the file: use system.io; 8. In the Solution Explorer, right-click the client project item. Then select "Set to Start Project" on the context menu. Back to top Try Operation 1. On the Debug menu, click Start. FORM1 will appear. 2. Click the button tagged as "Stored on the server". This will call the SaveDocument Web method. This web approach stores local documents in the