我有一个Sharepoint中的元素列表,他们有标题,描述和附件的选项。这是经典名单。Sharepoint,如何知道附件的千字节而不下载它?
我需要列出所有这些项目,显示附件的千字节。
的问题是,我不能做一个FileInfo,因为我知道的唯一路径是URI路径: http://example.com/../attachments/delete.txt
我怎样才能知道KBS没有做,迫使我WebRequest的下载文件来获得这个信息。
我有一个Sharepoint中的元素列表,他们有标题,描述和附件的选项。这是经典名单。Sharepoint,如何知道附件的千字节而不下载它?
我需要列出所有这些项目,显示附件的千字节。
的问题是,我不能做一个FileInfo,因为我知道的唯一路径是URI路径: http://example.com/../attachments/delete.txt
我怎样才能知道KBS没有做,迫使我WebRequest的下载文件来获得这个信息。
通常,您可以通过创建HEAD请求来获取来自通用URI的文件大小,该HEAD请求仅获取标题,并查看响应中的Content-Length标题。
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("https://stackoverflow.com/robots.txt");
req.Method = "HEAD";
System.Net.WebResponse resp = req.GetResponse();
int ContentLength;
if(int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
{
//Do something useful with ContentLength here
}
试试这个:
using (SPSite site = new SPSite("http://fsm/Lists/sample/AllItems.aspx"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["sample"];
SPListItemCollection items = list.GetItems(new SPQuery());
foreach (SPListItem item in items)
{
foreach (string attachment in item.Attachments)
{
// "http://fsm" + "Lists/sample/1_.000"
Uri itemAddress = new Uri(new Uri(web.Url), item.Url);
Uri attachmentAddress = new Uri(itemAddress,
String.Format("Attachments/{0}/{1}", item.ID, attachment));
SPFile file = web.GetFile(attachmentAddress.AbsoluteUri);
Console.WriteLine("{0} have {1} bytes", file.Name, file.Length);
}
}
}
}
感谢您的信息,但它不是SPDocumentLibrary中的文件,它是列表项中的附件,您可以检索与:SPAttachmentCollection attachs = items [0]。附件; 有什么想法? – netadictos 2009-10-08 12:44:19
web.GetFile(item.Attachments [0])。长度? – 2009-10-08 13:03:25
这会给你名字文件的字符长度 – netadictos 2009-10-08 15:16:01
这取决于你的目标Web服务器是如何配置的,当然,你不包括在此的任何细节。如果您控制目标网络服务器和您提供的文件系统,则可能有更简单的方法来执行此操作 – 2009-10-08 12:03:45