Category: C# General

1. Create a web service project and add the following code:

    using System.IO;

    [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;	
    } 

2. Create a directory C:DocumentDirectory

Now for the client:

1. Create a windows application project in the same project as the web service and create a web reference to it called “localhost”
2. Add 2 buttons called “cmdSend” and “cmdGet” and implement the click events.

	using System.IO;
	private void Form1_Load(object sender, EventArgs e)
        {

        }
        string sFile = "c:test.txt";

        private void cmdGet_Click(object sender, System.EventArgs e)
        {
            MemoryStream objstreaminput = new MemoryStream();
            FileStream objfilestream = new FileStream(sFile.Insert(sFile.LastIndexOf("."), "2"), FileMode.Create, FileAccess.ReadWrite);

            localhost.Service myservice = new localhost.Service();
            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();

        }

        private void cmdSend_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.Service myservice = new localhost.Service();
            myservice.SaveDocument(mybytearray, sFile.Remove(0, sFile.LastIndexOf("") + 1));
            objfilestream.Close();
        }

3. Create a file c:test.txt
4. Click the “Send” button. The file will be sent to c:DocumentDirectory
5. Click the “Get” button. The file will be pulled from c:DocumentDirectory and saved in c:test2.txt

 

Tags:

C#

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.