如何从Microsoft.SharePoint.Client.File对象获取文件大小?

时间:2011-12-01 10:03:37

标签: c# .net sharepoint sharepoint-2010

我正在寻找一种从Microsoft.SharePoint.Client.File对象获取文件大小的好方法。

Client对象没有Length成员。

我试过了:

foreach (SP.File file in files)
{
    string path = file.Path;
    path = path.Substring(this.getTeamSiteUrl().Length);
    FileInformation fileInformation = SP.File.OpenBinaryDirect(this.Context, path);
    using (MemoryStream memoryStream = new MemoryStream())
    {
        CopyStream(fileInformation.Stream, memoryStream);
        file.Size = memoryStream.Length;
    }
}

通过使用MemoryStream给了我一个长度,但它对性能不利。此文件也不属于文档库。由于它是附件,我无法使用ListItem将其转换为ListItemAllFields对象。如果我可以将其转换为ListItem,我可以使用ListItem["File_x0020_Size"]

获取其大小

如何使用C#获取SharePoint中Client对象的文件大小?

3 个答案:

答案 0 :(得分:4)

加载File_x0020_Size字段信息以获取它。

当我想列出Sharepoint 2010文件夹中的所有文件时,我就会这样做:

//folderPath is something like /yoursite/yourlist/yourfolder
Microsoft.SharePoint.Client.Folder spFolder = _ctx.Web.GetFolderByServerRelativeUrl(folderPath);

_ctx.Load(spFolder);
_ctx.ExecuteQuery();

FileCollection fileCol = spFolder.Files;
_ctx.Load(fileCol);
_ctx.ExecuteQuery();

foreach (Microsoft.SharePoint.Client.File spFile in fileCol)
{
    //In here, specify all the fields you want retrieved, including the file size one...
    _ctx.Load(spFile, file => file.Author, file => file.TimeLastModified, file=>file.TimeCreated, 
                            file => file.Name, file => file.ServerRelativeUrl, file => file.ListItemAllFields["File_x0020_Size"]);
    _ctx.ExecuteQuery();

    int fileSize = int.Parse((string)spFile.ListItemAllFields["File_x0020_Size"]);
}

_ctx显然是你发起的ClientContext

以下是所有Sharepoint内部字段的extended list

答案 1 :(得分:0)

你不能只使用Stream属性的长度吗?

file.Size = fileInformation.Stream.Length;

答案 2 :(得分:0)

我不知道这个问题是否得到解决,但是对于那些正在寻找答案的人(就像我一样):

...获取SP.FILE的代码......

SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(ctx, mySPFile.ServerRelativeUrl);
byte[] bodyString = ReadToEnd(fileInfo.Stream);
int length = bodyString.Length;
Console.Write(length.ToString());

......做你需要做的其他事......

    public static byte[] ReadToEnd(System.IO.Stream stream)
    {
        long originalPosition = 0;

        if (stream.CanSeek)
        {
            originalPosition = stream.Position;
            stream.Position = 0;
        }

        try
        {
            byte[] readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            if (stream.CanSeek)
            {
                stream.Position = originalPosition;
            }
        }
    }

希望这会对其他人有所帮助,因为我无法在互联网上找到直接答案!