How to download files from datalistview?
when i clicking link button in datalist view the file should be download.can any one give me the reference..
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 28, 2013, 12:52 AM EDT
selva kumarRank #2,987 · 483 reputationPosted Oct 28, 2013, 1:01 AM EDT
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 28, 2013, 1:36 AM EDT
Try something like this when you click on that link,
Response.ContentType = "application/pdf"; //Response.ContentType = "Application/msword";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf"));
Response.End();
selva kumarRank #2,987 · 483 reputationPosted Oct 28, 2013, 2:44 AM EDT
Source:
Font-Names="Verdana" Font-Size="Small" GridLines="Horizontal" RepeatDirection="Horizontal" RepeatColumns="0"
Width="700px" ForeColor="Black" Height="6px" UseAccessibleHeader="True" OnItemCommand="DataList1_ItemCommand">
File Name:
File Type :
File Size :
Download:
<%--
C#:
public partial class Staff_Uploadbooks : System.Web.UI.Page
{
SqlConnection Con;
SqlCommand Cmd;
SqlDataAdapter Sdap;
protected void Page_Load(object sender, EventArgs e)
{
Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=studentmgmt;Integrated Security=True");
if (!IsPostBack)
{
datalistbind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filetype = Path.GetExtension(FileUpload1.PostedFile.FileName);
Stream str = FileUpload1.PostedFile.InputStream;
int size1 = (int)str.Length;
double doubl = Convert.ToDouble(size1);
double size2 = doubl / 1048576;
Label size = new Label();
size.Text = Convert.ToString(size2) + "MB";
Cmd = new SqlCommand("insert into Staffuploads(FileName,ContentType,Size) values(@FileName,@ContentType,@Size)", Con);
Cmd.Parameters.AddWithValue("@FileName", filename);
Cmd.Parameters.AddWithValue("@ContentType", filetype);
Cmd.Parameters.AddWithValue("@Size", size.Text);
Cmd.Connection = Con;
Con.Open();
Cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
Con.Close();
datalistbind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "click")
{
string file = e.CommandArgument.ToString();
}
}
public void datalistbind()
{
try
{
Con.Open();
Sdap = new SqlDataAdapter("Select T_ID,[Filename],ContentType,Substring([Size],1,4) Size From Staffuploads", Con);
DataSet Ds = new DataSet();
Sdap.Fill(Ds);
DataList1.DataSource = Ds.Tables[0];
DataList1.DataBind();
}
catch
{
}
finally
{
Con.Close();
}
}
}
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 28, 2013, 3:11 AM EDT
What is T_ID?
selva kumarRank #2,987 · 483 reputationPosted Oct 28, 2013, 3:46 AM EDT
here is my table struct.all fields are declared as varchar,T_Id as int identy(1,1)..
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 28, 2013, 4:16 AM EDT
i checked your code . In that you are not storing the file any where. you are just taking the name,size and extension of the file and storing it to db.
so its not storing the file physically. for that you have to store your file in your application in a folder or you can store the file in db.
try to store the file in application folder . then you can able to download it from datalist by passing the path of the file.
refer to this link
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html