Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
}

//OpenFile(attachmentURL);
Response.Redirect(attachmentURL);

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?

UPDATE

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;
                }
            }
            OpenFile(attachmentURL);

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

            return attachmentURL;

            //Response.Redirect(attachmentURL);
            //Response.Redirect("http://so4:60000/SitePages/Capability%20Presentation.aspx");
        }
    }
}

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!

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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;

share|improve this answer
add comment

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];
share|improve this answer
    
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 '11 at 10:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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