Tuesday, April 19, 2005

Downloading a file from Sharepoint 2

// C#
public void DownloadDocument(string localFile,string remoteFile)
{
try
{
// Create the web request object
WebRequest request = WebRequest.Create(remoteFile);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "GET";
// Get a web response back
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();
FileStream fstream = new FileStream(localFile, FileMode.OpenOrCreate, FileAccess.Write);
byte [] buffer = new byte[1024];
int bytesRead = 0;
do
{
bytesRead = reader.Read(buffer,0,1024);
fstream.Write(buffer,0,bytesRead);
} while(bytesRead >0);
response.Close();
fstream.Close();
}
catch(Exception ex)
{
MessageBox.Show(host.HostForm,ex.Message,"Error Downloading",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

No comments:

 
Jeffrey Ting Jeffrey Ting on Facebook Jeffrey Ting on Spock Jeffrey Ting on Plaxo Jeffrey Ting on Spoke Jeffrey Ting on LinkedIn