gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:33:50 26 4
gpt4 key购买 nike

我正在尝试从文档库中某个文件夹的子文件夹中下载一个 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.");
}
}

也许有人有想法?

问候

最佳答案

试试这个:

使用 FileInformation并获取内存流

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 数组

 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();
}
}

关于c# - 带有 camlQuery 的 Sharepoint 2010 客户端对象模型 - 文件下载但无内容/0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10024524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com