I have an Image button in a custom 2010 webpart, on click of that I would like to download a file.

Code Used :

foreach (string filename in item.Attachments)
    attachmentURL = SiteListURL + "attachments/" + item.ID + "/" + filename;


Here Openfile throws an error -

The file http://so4:60000/Lists/AbilityTableSales/attachments/1/Dashboard Solutions.pptx does not exist.

Though the line Response.Redirect(attachmentURL); working and it is opening the save dialogue box. If I click the image button again the event is not firing, I guess it is because I used Response.Redirectmethod.

How can I do this?


Entire Code

private string getlist(string Title)
    string SiteListURL = "http://so4:60000/Lists/CapabilityTableSales/";
    using (SPSite oSiteCollection = new SPSite(SiteListURL))
        using (SPWeb web = oSiteCollection.OpenWeb())
            SPQuery query = new SPQuery();

            query.Query = @"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+Title+"</Value></Eq></Where>"; 
            //query.RowLimit = 6;
            SPList list = web.Lists["CapabilityTableSales"];
            SPListItemCollection col = list.GetItems(query);
            int Id = -1 ;
            foreach (SPListItem LI in col)
                Id = Convert.ToInt32(LI["ID"]);
            string attachmentURL="";
            if (Id != -1)
                SPListItem item = list.GetItemById(Id);

                foreach (string filename in item.Attachments)
                    attachmentURL = SiteListURL + "attachments/" + item.ID + "/" + filename;

            //System.Net.WebClient _WebClient = new System.Net.WebClient();
            //// Downloads the resource with the specified URI to a local file.

            return attachmentURL;


Code for Button click :

protected void ImageButton11_Click(object sender, ImageClickEventArgs e)
    string redirectURL11 = getlist("1.1");

    ImageButton11.PostBackUrl = redirectURL11;

First time it will work properly, the save dialogue box will appear. However 2nd time its just not working until I refresh the page. Another problem is that in this page I have got another web part - no relation to this one. Even that button will not fire!

2 Answers 2


Got it worked !! Used an anchor control with Image instead of Image Button!!

<a href="" runat="server" id="aAttachmentURL11" target="_blank"> <img src="~/_layouts/images/ipw/PowerPoint-64.png" alt="" style="border: 0px" width="63px" /> </a>

Code : And then called the method getlist(string Title) to return the related attachment URL!!

string redirectURL11 = getlist("1.1"); aAttachmentURL11.HRef = redirectURL11;


You can construct the attachment URL by using the SPAttachmentCollection.UrlPrefix property of the attachments collection. For example:

string attachmentUrl = item.Attachments.UrlPrefix + item.Attachments[0];
  • Thanks Matthias for this great info, however my attachment url I constructed is working well, my question is using that url, how can I download or open the file from C# or code behind on button click?
    – Jithu
    Nov 25, 2011 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.