Microsoft KB Archive/318425

= HOW TO: Send and Receive Binary Documents by Using an ASP.NET Web Service and Visual C# .NET =

Article ID: 318425

Article Last Modified on 12/15/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q318425



IN THIS TASK
SUMMARY
 * Build the Web Service
 * Build a Client for the Web Service
 * Try It Out

REFERENCES



SUMMARY
This step-by-step article shows you how to build a Web service and a Web service client to send and receive binary documents by using Microsoft ASP.NET and Microsoft Visual C# .NET. You can use ASP.NET and Visual C# .NET to build a Web service that both saves binary documents to a folder on a Web server and retrieves binary documents from a folder on a Web server. You can use this service as a simple Document Management System on the Web.

back to the top

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 = &quot;C:\\DocumentDirectory\\&quot; + 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 = &quot;C:\\DocumentDirectory\\&quot; + 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 = &quot;C:\\DocumentDirectory\\&quot; + 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 :\\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.</li> Make sure that the SaveDocument, GetDocument, and GetDocumentLen methods appear.</li> Close the Web browser window to stop debugging.</li></ol> </li></ol>

back to the top

Build a Client for the Web Service
<ol> On the File menu in Visual Studio .NET, click Add Project, and then click New Project.</li> In the Visual C# Projects list, select Windows Application, and then click OK. By default, Form1 is created.</li> Add a Web reference to the Web service, as follows: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click the client project item. Then select Add Web Reference on the Context menu.</li> 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.</li> In the Add Web Reference dialog box, click Add Reference.</li></ol> </li> 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.</li> Double-click button1 and button2 to create default Click event handlers for the buttons.</li>  Replace the handlers with the following code: string sFile = &quot; &quot;;

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(&quot;\\&quot;)+1)); objfilestream.Close; }

private void button2_Click(object sender, System.EventArgs e) { MemoryStream objstreaminput = new MemoryStream; FileStream objfilestream = new FileStream(sFile.Insert(sFile.LastIndexOf(&quot;.&quot;),&quot;2&quot;), FileMode.Create,FileAccess.ReadWrite); localhost.Service1 myservice = new localhost.Service1; int len = (int)myservice.GetDocumentLen(sFile.Remove(0,sFile.LastIndexOf(&quot;\\&quot;)+1)); Byte[] mybytearray = new Byte[len]; mybytearray = myservice.GetDocument(sFile.Remove(0,sFile.LastIndexOf(&quot;\\&quot;)+1)); objfilestream.Write(mybytearray,0,len); objfilestream.Close; } 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.

</li>  Add the following namespace at the beginning of the file: using System.IO; </li> In Solution Explorer, right-click the client project item. Then select Set as Startup Project on the Context menu.</li></ol>

back to the top

Try It Out

 * 1) On the Debug menu, click Start. Form1 appears.
 * 2) 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 :\DocumentDirectory\ folder on the server. After you have transferred the document, verify that the file exists in the destination folder.
 * 3) Click the button labeled Retrieve Document from the Server. This will call the GetDocument Web method. This Web method retrieves the document from the :\DocumentDirectory\ folder on the server. The document is saved on the local drive that is specified in the code.

back to the top

<div class="references_section">