簡體   English   中英

如何從Microsoft.SharePoint.Client.File對象獲取文件大小?

[英]How do I get the filesize from the Microsoft.SharePoint.Client.File object?

我正在尋找一種從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 ,但是對性能不好。 該文件也不屬於文檔庫。 由於它是附件,因此無法使用ListItemAllFields將其轉換為ListItem對象。 如果可以將其轉換為ListItem ,則可以使用: ListItem["File_x0020_Size"]獲得其大小。

如何使用C#在SharePoint中獲取Client對象的文件大小?

加載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內部字段的擴展列表

我不知道這個問題是否曾經解決過,但是對於正在尋找答案的人們(就像我一樣):

...用於獲取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;
            }
        }
    }

希望這對其他人有幫助,因為我在互聯網上找不到直接的答案!

您不能只使用Stream屬性的長度嗎?

file.Size = fileInformation.Stream.Length;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM