Upload file to public folder
- Hi there
How can I upload a doc file to a public folder programmatically?
Thanks In Advance
Answers
You dont need to covert it to a string all you need to do is read the byte stream set the content type to application/octet-stream and the post the byte stream. eg
Code Snippetstatic void Main(string[] args)
{
System.Net.HttpWebRequest PUTRequest;
System.Net.WebResponse PUTResponse;
System.Net.HttpWebRequest MOVERequest;
System.Net.WebResponse MOVEResponse;
System.Net.CredentialCache MyCredentialCache;
string pfURI = "";
string strUserName = "user";
string strPassword = "password";
string strDomain = "domain";
System.IO.Stream PUTRequestStream = null;try
{
String emFileName = @"c:\Lesson18_Word_List.xls";
FileStream fsFileStream = new FileStream(emFileName, FileMode.Open, FileAccess.Read);
byte[] bdBinaryData1 = ReadFully(fsFileStream, fsFileStream.Length);pfURI = "http://servername/public/folder/Lesson18_Word_List.xls";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(pfURI),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(pfURI);
PUTRequest.Credentials = MyCredentialCache;
PUTRequest.Method = "PUT";
PUTRequest.ContentLength = bdBinaryData1.Length;
PUTRequestStream = PUTRequest.GetRequestStream();
PUTRequestStream.Write(bdBinaryData1, 0, bdBinaryData1.Length);
PUTRequestStream.Close();
PUTRequest.ContentType = "application/octet-stream";
PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
PUTResponse.Close();
}
catch (Exception ex)
{
// Catch any exceptions. Any error codes from the PUT
// or MOVE method requests on the server will be caught
// here, also.
Console.WriteLine(ex.Message);
}}
public static byte[] ReadFully(Stream stream, long initialLength)
{
// ref Function from http://www.yoda.arachsys.com/csharp/readbinary.html
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}byte[] buffer = new byte[initialLength];
int read = 0;int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}Cheers
Glen
All Replies
- What version of Exchange are you using 2000/3 or 2007 ?
Do you want to upload the file as an attachment to a post or upload it as a freedoc ?
To access an Exchange Mailbox or Public Folder you need to use one of the Exchange API's these consist of Mapi (via the OOM or CDO 1.2), CDOEX/Exoledb, WebDAV and on Exchange 2007 Exchange Web Services. You can find details and sample of using these API's in the Exchange SDK which you can download from http://msdn.microsoft.com/exchange
Cheers
Glen - Thanks Glen for your answer, my exchange version is 2003 and I want to upload the file as a free doc. I‘ve used WebDAV with ASP.net but it doesn’t work, I’ve read somewhere that webDAV Copy and move method is only used to copy and move files in the same data store and my original file is on an application server not on the exchange data store. Any Idea?
Thanks In Advance Thats correct about Copy and Move but all you really need to do is a put eg this is really simple example of puting a file into a public folder from a file share.
Code Snippetset strm = createobject("ADODB.Stream")
strm.Type = 1
strm.Open
strm.LoadFromFile "c:\Lesson18_Word_List.xls"
set req = createobject("microsoft.xmlhttp")
req.Open "PUT", "http://servername/public/folder/Lesson18_Word_List.xls",false,"domain\user","password"
req.setRequestHeader "Translate", "f"
req.setRequestHeader "Content-Type", "application/octet-stream"
req.send strm.read
wscript.echo req.responsetextCheers
Glen
- Thanks for your answer, can you give me the code snippet in vb.net?
- I am still waiting for the answer.Any idea?
- Sorry i dont have a VB.NET sample I generally allways code in C# i would suggest taking a look at the samples in the SDK such as http://msdn2.microsoft.com/en-us/library/ms878112(EXCHG.65).aspx. This is very simple code all you need is something to read the file into a binary stream and then post the stream. Have a go at it yourself if you have problem post the code your having problems with.
Cheers
Glen - The problem is that my doc is a smart document which consists of expansion pack and ..., I dont think if I can convert the file completely to a string, so that I can use WebDAV Put Method. Any suggection?
You dont need to covert it to a string all you need to do is read the byte stream set the content type to application/octet-stream and the post the byte stream. eg
Code Snippetstatic void Main(string[] args)
{
System.Net.HttpWebRequest PUTRequest;
System.Net.WebResponse PUTResponse;
System.Net.HttpWebRequest MOVERequest;
System.Net.WebResponse MOVEResponse;
System.Net.CredentialCache MyCredentialCache;
string pfURI = "";
string strUserName = "user";
string strPassword = "password";
string strDomain = "domain";
System.IO.Stream PUTRequestStream = null;try
{
String emFileName = @"c:\Lesson18_Word_List.xls";
FileStream fsFileStream = new FileStream(emFileName, FileMode.Open, FileAccess.Read);
byte[] bdBinaryData1 = ReadFully(fsFileStream, fsFileStream.Length);pfURI = "http://servername/public/folder/Lesson18_Word_List.xls";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(pfURI),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(pfURI);
PUTRequest.Credentials = MyCredentialCache;
PUTRequest.Method = "PUT";
PUTRequest.ContentLength = bdBinaryData1.Length;
PUTRequestStream = PUTRequest.GetRequestStream();
PUTRequestStream.Write(bdBinaryData1, 0, bdBinaryData1.Length);
PUTRequestStream.Close();
PUTRequest.ContentType = "application/octet-stream";
PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
PUTResponse.Close();
}
catch (Exception ex)
{
// Catch any exceptions. Any error codes from the PUT
// or MOVE method requests on the server will be caught
// here, also.
Console.WriteLine(ex.Message);
}}
public static byte[] ReadFully(Stream stream, long initialLength)
{
// ref Function from http://www.yoda.arachsys.com/csharp/readbinary.html
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}byte[] buffer = new byte[initialLength];
int read = 0;int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}Cheers
Glen
- Thanks, the above solution has solved my problem.
- Hi All
I used this method, Works great. But the document is posting with out senders name. The 'From' Remains blank. Do we have a workarround this?
Thanks in advance - Nice sample Glen. Thanks!
Dave


