带有camlQuery的Sharepoint 2010客户端对象模型-文件下载但没有内容/0字节

本文关键字:0字节 文件下载 camlQuery Sharepoint 2010 对象模型 客户端 带有 | 更新日期: 2023-09-27 18:30:00

我正试图从文档库文件夹中的子文件夹下载一个txt文件。

我正在使用camlQuery来实现这一点。不幸的是,我没有得到txt文件的内容。它有0个字节。

public void SaveFolderFiles(string fileName, string libraryName, ClientOM.ClientContext clientContext)
    {
        ClientOM.List sharedDocumentsList = clientContext.Web.Lists.GetByTitle(libraryName);
        ClientOM.CamlQuery camlQuery = new ClientOM.CamlQuery();
        camlQuery.FolderServerRelativeUrl = "/Site/Folder/Folder2010/";
        camlQuery.ViewXml =
            @"<View>
            <Query>
              <Where>
                <Eq>
                  <FieldRef Name='FileLeafRef'/>
                  <Value Type='Text'>" + fileName + @"</Value>
                </Eq>
              </Where>
              <RowLimit>1</RowLimit>
            </Query>
          </View>";
        ClientOM.ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
        clientContext.Load(sharedDocumentsList);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();
        if (listItems.Count == 1)
        {
            ClientOM.ListItem item = listItems[0];
            Console.WriteLine("FileLeafRef: {0}", item["FileLeafRef"]);
            Console.WriteLine("FileDirRef: {0}", item["FileDirRef"]);
            Console.WriteLine("FileRef: {0}", item["FileRef"]);
            Console.WriteLine("File Type: {0}", item["File_x0020_Type"]);
            ClientOM.FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                fileInformation.Stream.CopyTo(memoryStream);
                using (FileStream fileStream = File.Create(@"D:'" + item["FileLeafRef"].ToString()))
                {
                    memoryStream.CopyTo(fileStream);
                }
                memoryStream.Flush();
            }

        }
        else
        {
            Console.WriteLine("Document not found.");
        }
    }

也许有人有主意?

问候

带有camlQuery的Sharepoint 2010客户端对象模型-文件下载但没有内容/0字节

试试这个:

使用FileInformation获取MemoryStream

string fileurl = (string)liitem["FileRef"];
FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileurl);
byte[] bytesarr = ReadFully(ffl.Stream);
MemoryStream mnm = new MemoryStream(bytesarr);

ReadFully函数,用于将Stream转换为Bytes array

 public byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

直接导出到输出文件。。。我认为这是最简单的方法

FileInformation fInfo = File.OpenBinaryDirect(currentSiteContext, ServerRelativeURL);
System.IO.FileStream outPutFile = System.IO.File.OpenWrite(string.Concat(OutputPath, "''", DocumentName));
fInfo.Stream.CopyTo(outPutFile);
fInfo.Stream.Close();
outPutFile.Close();