2009-10-08 48 views

回答

1

通常,您可以通过创建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 
} 

code from this stackoverflow question

+0

这取决于你的目标Web服务器是如何配置的,当然,你不包括在此的任何细节。如果您控制目标网络服务器和您提供的文件系统,则可能有更简单的方法来执行此操作 – 2009-10-08 12:03:45

3

试试这个:

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); 
      } 
     } 
    } 
} 
+0

感谢您的信息,但它不是SPDocumentLibrary中的文件,它是列表项中的附件,您可以检索与:SPAttachmentCollection attachs = items [0]。附件; 有什么想法? – netadictos 2009-10-08 12:44:19

+2

web.GetFile(item.Attachments [0])。长度? – 2009-10-08 13:03:25

+0

这会给你名字文件的字符长度 – netadictos 2009-10-08 15:16:01