[C#.NET][Sharepoint 2013] 如何利用 CSOM 下載特定檔案

[C#.NET][Sharepoint 2013] 如何利用 CSOM 下載特定檔案

續上篇:http://www.dotblogs.com.tw/yc421206/archive/2014/06/03/145354.aspx

本文章節:

前置作業,開啟清單版本控制

下載最新版本檔案:

下載特定版本檔案:


前置作業,開啟清單版本控制

image

image

image

 

 

下載最新版本檔案:

當清單裡面有一個檔案,可按下列步驟來下載檔案

  1. 搜尋檔案,var file = list.RootFolder.Files.GetByUrl("demo.docx")並取
  2. 得檔案路徑 file.ServerRelativeUrl
  3. 開啟檔案 Microsoft.SharePoint.Client.File.OpenBinaryDirect ,取得 Stream。
  4. 存檔。

程式碼如下:

{
    using (ClientContext client = new ClientContext("http://sps2013/CsomApi"))
    {
        client.Credentials = new NetworkCredential("your account", "your password");
        var site = client.Web;
        var list = site.Lists.GetByTitle("Documents");
        var file = list.RootFolder.Files.GetByUrl("demo.docx");

        client.Load(file);
        client.ExecuteQuery();

        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(client, file.ServerRelativeUrl);

        byte[] readbuffer = null;
        using (var memory = new MemoryStream())
        {
            while (true)
            {
                byte[] buffer = new byte[1024];
                var length = fileInfo.Stream.Read(buffer, 0, buffer.Length);
                if (length <= 0)
                {
                    break;
                }
                memory.Write(buffer, 0, length);
            }

            memory.Seek(0, SeekOrigin.Begin);
            readbuffer = memory.ToArray();
        }
        var targetStream = new FileStream("demo-1.docx", FileMode.Create, FileAccess.Write);
        targetStream.Write(readbuffer, 0, readbuffer.Length);
        targetStream.Dispose();
        fileInfo.Dispose();
    }
}

 

 

下載特定版本檔案:

欲想下載特殊版本,可按下列步驟來下載檔案

  1. 取得檔案 Versions,這裡會記錄舊的版本訊息,我會需要它的 Url 屬性
  2. 我找不到下載特殊版本的操作物件,所以先取得檔案路徑後,再用 HttpWebRequest 下載檔案,同樣可以達到下載特定版本的效果

程式碼如下:

{
    using (ClientContext client = new ClientContext("http://sps2013/CsomApi"))
    {
        client.Credentials = new NetworkCredential("your account", "your password");

        var site = client.Web;
        var list = site.Lists.GetByTitle("Documents");
        var file = list.RootFolder.Files.GetByUrl("demo.docx");

        var versions = file.Versions;
        client.Load(versions);
        client.ExecuteQuery();

        var findVersion = versions.FirstOrDefault(o => o.VersionLabel == "1.0");
        string url = client.Url + @"/" + findVersion.Url;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
        httpRequest.Credentials = new NetworkCredential("your account", "your password");
        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        Stream sourceStream = httpResponse.GetResponseStream();

        byte[] buffer = new byte[8 * 1024];
        FileStream targetStream = new FileStream("demo-1.docx", FileMode.Create, FileAccess.Write);
        int size = 0;

        do
        {
            size = sourceStream.Read(buffer, 0, buffer.Length);
            if (size > 0)

                targetStream.Write(buffer, 0, size);
        } while (size > 0);

        targetStream.Close();
        httpResponse.Close();
    }
}

版本歷程訊息如下圖:

image

 

 


本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/03/145357.aspx

 

 

 

 

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo