如何从 Microsoft.SharePoint.Client.File 对象获取文件大小? [英] How do I get the filesize from the Microsoft.SharePoint.Client.File object?
问题描述
我正在寻找一种从 Microsoft.SharePoint.Client.File
对象获取文件大小的好方法.
I'm looking for a good way to get filesize from the Microsoft.SharePoint.Client.File
object.
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"]
Which gave me a length through using the MemoryStream
, but it's not good for performance. 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
. If I could convert it to a ListItem
, I could get its size using: ListItem["File_x0020_Size"]
如何使用 C# 在 SharePoint 中获取 Client
对象的文件大小?
How do I get the filesize of the Client
object in SharePoint using C#?
推荐答案
加载File_x0020_Size
字段信息即可获取.
Load the File_x0020_Size
field information to get it.
当我想列出 Sharepoint 2010 文件夹中的所有文件时,我会这样做:
This is what I do when I want to list all the files in a Sharepoint 2010 folder:
//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
.
这篇关于如何从 Microsoft.SharePoint.Client.File 对象获取文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!