Search code examples
.netsharepointsharepoint-2007

Sharepoint, how to know the kilobytes of the attachment without downloading it?


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.


Solution

  • 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)