我想通过使用正则表达式下载多个文件,其中用户通过变量提供文件名,并在 caml 查询中传递,它可以循环访问列表 url。这个 caml 查询区域有点新。有什么建议如何实现它吗?
camlQuery.ViewXml = @"<View>
<Query>
<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + fileToDownload + @"</Value></Eq></Where>
</Query>
<ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields>
</View>";
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(web.Lists);
List list = clientContext.Web.GetList(listUrl);
//clientContext.Load(list);
//clientContext.ExecuteQuery();
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
Console.WriteLine("This is file");
}
else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
Console.WriteLine("This is folder");
}
camlQuery.ViewXml = @"<View>
<Query>
<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + fileToDownload + @"</Value></Eq></Where>
</Query>
<ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields>
</View>";
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(web.Lists);
List list = clientContext.Web.GetList(listUrl);
//clientContext.Load(list);
//clientContext.ExecuteQuery();
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
Console.WriteLine("This is file");
}
else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
Console.WriteLine("This is folder");
}
我需要从 FiletoDownload 变量上的匹配模式下载文件,就像它的 . 它应该下载所有文件。
以下是供您参考的代码片段:
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
Console.WriteLine("This is file");
clientContext.Load(item.File);
clientContext.ExecuteQuery();
var fileRef = item.File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
var fileName = Path.Combine(@"D:", (string)item.File.Name);
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
Console.WriteLine("This is folder");
}
}