Tuesday, April 19, 2005

Uploading a file to Sharepoint 2

//C#
public void UploadDocument(string localFile, string remoteFile)
{
// Read in the local file
FileStream fstream = new FileStream(localFile, FileMode.Open, FileAccess.Read);
byte [] buffer = new byte[fstream.Length];
fstream.Read(buffer, 0, Convert.ToInt32(fstream.Length));
fstream.Close();
try
{
// Create the web request object
WebRequest request = WebRequest.Create(remoteFile);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "PUT";
request.ContentLength = buffer.Length;
// Write the local file to the remote system
BinaryWriter writer = new BinaryWriter(request.GetRequestStream());
writer.Write(buffer, 0, buffer.Length);
writer.Close();
// Get a web response back
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
}
catch(Exception ex)
{
MessageBox.Show(host.HostForm,ex.Message,"Error Uploading");
}
}

No comments:

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