Build the Web Service
- On the File menu in Microsoft Visual Studio .NET, click New, and then click Project.
- In Visual C# Projects, select the ASP.NET Web Service. Type or paste http://localhost/DocumentManagementService for the Location, and then click OK. By default, Service1.asmx is created and is displayed in design view.
- On the View menu, click Code to display the code view for Service1.asmx.
- 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 Byte[len]; objfilestream.Read(documentcontents,0,len); objfilestream.Close(); return documentcontents; }
NOTE: The code saves the documents to the <root>:\\DocumentDirectory\\ directory path on the server. Change this to the folder on your Web server where you want to save the documents. - Add the following namespace to the beginning of the Service1.asmx:
using System.IO;
- Test the Web service:
- On the Debug menu, click Start to start the Web service. This starts the Web browser, and the Help page of the service description appears.
- Make sure that the SaveDocument, GetDocument, and GetDocumentLen methods appear.
- Close the Web browser window to stop debugging.
Build a Client for the Web Service
- On the File menu in Visual Studio .NET, click Add Project, and then click New Project.
- In the Visual C# Projects list, select Windows Application, and then click OK. By default, Form1 is created.
- Add a Web reference to the Web service, as follows:
- In Solution Explorer, right-click the client project item. Then select Add Web Reference on the Context menu.
- In the Add Web Reference dialog box, type the URL to the Web Services Description Language (WSDL) file for the Web Service, and then press ENTER.
NOTE: The default location for the WSDL file is http://localhost/DocumentManagementService/Service1.asmx?WSDL. - In the Add Web Reference dialog box, click Add Reference.
- Add two buttons to Form1. Set the Text property of button1 to Store Document on the Server. Set the Text property of button2 to Retrieve Document from the Server.
- Double-click button1 and button2 to create default Click event handlers for the buttons.
- Replace the handlers with the following code:NOTE: The sFile variable must contain the local file path to a document that will be uploaded to the server. When the document is downloaded, it is placed in the same folder, and a value of 2 is appended to the file name.
string sFile = "<file path>"; 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(object sender, System.EventArgs e) { MemoryStream objstreaminput = new MemoryStream(); 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(); }
- Add the following namespace at the beginning of the file:
using System.IO;
- In Solution Explorer, right-click the client project item. Then select Set as Startup Project on the Context menu.
Try It Out
- On the Debug menu, click Start. Form1 appears.
- Click the button labeled Store Document on the Server. This will call the SaveDocument Web method. This Web method saves the local document in the <root>:\DocumentDirectory\ folder on the server. After you have transferred the document, verify that the file exists in the destination folder.
- Click the button labeled Retrieve Document from the Server. This will call the GetDocument Web method. This Web method retrieves the document from the <root>:\DocumentDirectory\ folder on the server. The document is saved on the local drive that is specified in the code.