Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

Build the Web Service

  1. On the File menu in Microsoft Visual Studio .NET, click New, and then click Project.
  2. 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.
  3. On the View menu, click Code to display the code view for 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 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.

  5. Add the following namespace to the beginning of the Service1.asmx:
    using System.IO;
  6. Test the Web service:
    1. 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.
    2. Make sure that the SaveDocument, GetDocument, and GetDocumentLen methods appear.
    3. Close the Web browser window to stop debugging.

Build a Client for the Web Service

  1. On the File menu in Visual Studio .NET, click Add Project, and then click New Project.
  2. In the Visual C# Projects list, select Windows Application, and then click OK. By default, Form1 is created.
  3. Add a Web reference to the Web service, as follows:
    1. In Solution Explorer, right-click the client project item. Then select Add Web Reference on the Context menu.
    2. 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.
    3. In the Add Web Reference dialog box, click Add Reference.
  4. 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.
  5. Double-click button1 and button2 to create default Click event handlers for the buttons.
  6. Replace the handlers with the following code:
    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();
    }
    					
    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.

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

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 <root>:\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 <root>:\DocumentDirectory\ folder on the server. The document is saved on the local drive that is specified in the code.

↑ Back to the top


References

For more information, see the Microsoft Visual Studio .NET Documentation.

↑ Back to the top


Keywords: KB318425, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 318425
Revision : 6
Created on : 12/15/2003
Published on : 12/15/2003
Exists online : False
Views : 477