Create a web service project 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;
}
- Create a directory C:\DocumentDirectory
Now for the client#
- Create a windows application project in the same project as the web service and create a web reference to it, call it localhost
- 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();
}
- Create a file c:\test.txt
- Click Send button, file will be sent to c:\DocumentDirectory
- Click Get button, file will be pulled from c:\DocumentDirectory and saved in c:\test2.txt
back to c#
Add new attachment
Only authorized users are allowed to upload new attachments.
«
This page (revision-1) was last changed on 20-Jun-2007 07:49 by UnknownAuthor