问题描述
我正在寻找一个好方法从 Microsoft.SharePoint.Client.File
目标文件大小。
I'm looking for a good way to get filesize from the Microsoft.SharePoint.Client.File
object.
在客户端
对象没有一个长度
成员。
我尝试这样做:
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
。如果我可以将其转换为列表项
,我可以用得到它的大小:列表项[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"]
如何获得SharePoint中的客户端
对象的文件大小使用C#?
How do I get the filesize of the Client
object in SharePoint using C#?
推荐答案
我不知道,如果这个问题被解决过,但对于人谁是寻找一个答案(像我一样):
I don't know if this question was ever solved, but for the people who are looking for an answer (like I did) :
... $ C $下得到SP.FILE ...
... CODE FOR GETTING THE 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!
这篇关于我如何从Microsoft.SharePoint.Client.File对象的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!