仅使用 caml 查询动态下载文件而不是通过获取文件夹方法

Downloading file dynamically using caml query only not by get folder method

我想下载多个文件,比如使用正则表达式,用户通过变量提供文件名,然后在 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");
            }
        }