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.Redirect
method.
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!