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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Oct 28, 2013, 8:16 AM
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
selva kumarPosted Oct 28, 2013, 7:46 AM
here is my table struct.all fields are declared as varchar,T_Id as int identy(1,1)..
Jaganathan BantheswaranPosted Oct 28, 2013, 7:11 AM
What is T_ID?
selva kumarPosted Oct 28, 2013, 6:44 AM
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 BantheswaranPosted Oct 28, 2013, 5:36 AM
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 kumarPosted Oct 28, 2013, 5:01 AM
Jaganathan BantheswaranPosted Oct 28, 2013, 4:52 AM