简体   繁体   English

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

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

I'm looking for a good way to get filesize from the Microsoft.SharePoint.Client.File object. 我正在寻找一种从Microsoft.SharePoint.Client.File对象获取文件大小的好方法。

The Client object does not have a Length member. Client对象没有Length成员。

I tried this: 我尝试了这个:

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

Which gave me a length through using the MemoryStream , but it's not good for performance. 这给了我使用MemoryStream ,但是对性能不好。 This file also does not belong to a document library. 该文件也不属于文档库。 Since it's an attached file, I can't convert it to a ListItem object using ListItemAllFields . 由于它是附件,因此无法使用ListItemAllFields将其转换为ListItem对象。 If I could convert it to a ListItem , I could get its size using: ListItem["File_x0020_Size"] 如果可以将其转换为ListItem ,则可以使用: ListItem["File_x0020_Size"]获得其大小。

How do I get the filesize of the Client object in SharePoint using C#? 如何使用C#在SharePoint中获取Client对象的文件大小?

Load the File_x0020_Size field information to get it. 加载File_x0020_Size字段信息以获取它。

This is what I do when I want to list all the files in a Sharepoint 2010 folder: 当我要列出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 is obviously the ClientContext you have initiated. _ctx显然是您已启动的ClientContext

Here's an extended list of all Sharepoint internal fields 这是所有Sharepoint内部字段的扩展列表

I don't know if this question was ever solved, but for the people who are looking for an answer (like I did) : 我不知道这个问题是否曾经解决过,但是对于正在寻找答案的人们(就像我一样):

... CODE FOR GETTING THE SP.FILE ... ...用于获取SP.FILE的代码...

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

... DO THE OTHER STUFF YOU NEED TO DO .... ...做您需要做的其他事情....

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

Hope this will help other people, because I couldn't find a direct answer on the internet! 希望这对其他人有帮助,因为我在互联网上找不到直接的答案!

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

file.Size = fileInformation.Stream.Length;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Microsoft.Sharepoint.Client.ListItem 转换为 Microsoft.Sharepoint.Client.File? - How to convert from Microsoft.Sharepoint.Client.ListItem to Microsoft.Sharepoint.Client.File? 如何从类Microsoft.SharePoint.Client.ListItem中获取字段类型(整数,日期时间,选择)? - how do you get the field type (integer, datetime, choice) from the class Microsoft.SharePoint.Client.ListItem? 如何注册Microsoft.SharePoint.DLL文件? - How do I register Microsoft.SharePoint.DLL file? 如何使用SharePoint客户端对象模型获取当前网站? - How do I get the current web using SharePoint client object model? 使用Microsoft.SharePoint.Client从OneDrive下载文件 - download file from OneDrive using Microsoft.SharePoint.Client 如何从Microsoft.SharePoint.Client.ListItem检索所有属性? - How to retrieve all properties from Microsoft.SharePoint.Client.ListItem? 如何从 Microsoft.Sharepoint.Client.ZC6E190B28404933C48EZCE895E 中填充 Web class - How to shim the Web class from Microsoft.Sharepoint.Client.Web? 如何从Microsoft Word文件将嵌入的图像转换为PNG格式? - How do I get an embedded image into PNG format from a Microsoft Word File? 如何使用SharePoint 2010客户端对象模型获取阻止的文件扩展详细信息? - How to get Blocked File Extensions details using SharePoint 2010 Client Object Model? 如何以编程方式从sharepoint站点下载文件? - How do I programatically download a file from a sharepoint site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM