文件下载,但没有内容/ 0字节 - 与camlQuery的SharePoint 2010客户端对象模型 [英] Sharepoint 2010 client object model with camlQuery - file download but no content / 0 byte

查看:204
本文介绍了文件下载,但没有内容/ 0字节 - 与camlQuery的SharePoint 2010客户端对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个文档库中的文件夹内的子文件夹的TXT文件。

I'm trying to download a txt file from a subfolder within a folder in a document library.

我使用camlQuery实现这一目标。不幸的是,我没有得到任何txt文件的内容。它具有0字节。

I'm using camlQuery to achieve this. Unfortunately, i get no content of the txt file. It has 0 byte.

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.");
        }
    }



也许有人有一个想法?

Maybe someone has an idea?

问候

推荐答案

试试这个:

使用 FileInformation 并获得的MemoryStream

using FileInformation and get the 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功能,转换字节数组

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

这篇关于文件下载,但没有内容/ 0字节 - 与camlQuery的SharePoint 2010客户端对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆