0
votes

I have a list of elements in Sharepoint, they have title, description and the option to have an attachment. It's the classic list.

I need to make a list of all these items, showing the kilobytes of the attachment.

The problem is that I cannot do a FileInfo, because the only path I know is the URI path: http://example.com/../attachments/delete.txt

How can i know the kbs without doing a webrequest that forces me to download the file to get this info.

2

2 Answers

3
votes

Try this:

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);
            }
        }
    }
}
1
votes

You can usually get the file size from a generic URI by making a HEAD request which fetches only the headers, and looking at the Content-Length header in the response.

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)