Ask a questionAsk a question
 

AnswerUpload file to public folder

Answers

  • Wednesday, April 16, 2008 6:04 AMGlen ScalesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

      static 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

  • Monday, April 07, 2008 5:46 AMGlen ScalesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Monday, April 07, 2008 10:35 AMEntrancee80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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



  • Tuesday, April 08, 2008 5:04 AMGlen ScalesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 Snippet

    set 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.responsetext

     

     

     

    Cheers

    Glen

     

  • Tuesday, April 08, 2008 7:31 AMEntrancee80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your answer, can you give me the code snippet in vb.net?
  • Sunday, April 13, 2008 5:44 AMEntrancee80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am still waiting for the answer.Any idea?
  • Sunday, April 13, 2008 8:04 AMGlen ScalesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, April 16, 2008 5:25 AMEntrancee80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Wednesday, April 16, 2008 6:04 AMGlen ScalesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

      static 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

     

  • Sunday, April 20, 2008 12:17 PMEntrancee80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks, the above solution has solved my problem.
  • Monday, November 02, 2009 2:32 PMShiju Kadamala Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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


  • Wednesday, November 25, 2009 11:32 PMDaveBrask Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Nice sample Glen.  Thanks!
    Dave