IT 专业人士的资源 > 论坛主页 > Development > Upload file to public folder
提出问题提出问题
 

已答复Upload file to public folder

答案

  • 2008年4月16日 6:04Glen ScalesMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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

     

全部回复

  • 2008年4月7日 5:46Glen ScalesMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

  • 2008年4月7日 10:35Entrancee80 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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



  • 2008年4月8日 5:04Glen ScalesMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

     

  • 2008年4月8日 7:31Entrancee80 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks for your answer, can you give me the code snippet in vb.net?
  • 2008年4月13日 5:44Entrancee80 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I am still waiting for the answer.Any idea?
  • 2008年4月13日 8:04Glen ScalesMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

  • 2008年4月16日 5:25Entrancee80 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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?
  • 2008年4月16日 6:04Glen ScalesMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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

     

  • 2008年4月20日 12:17Entrancee80 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks, the above solution has solved my problem.
  • 2009年11月2日 14:32Shiju Kadamala 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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


  • 2009年11月25日 23:32DaveBrask 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Nice sample Glen.  Thanks!
    Dave