Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi friends,
I want to download files and the file path is in database. by using grid view if I select the specific file in gridview I want to download the data.

I am using link button in grid view to download files

my code is given below
public void filedownload()
    {
        con.Open();
        string command="select * from tblname where int_filename="+FileUpload.FileName+"";
        string path = Server.MapPath("images\\" + command);
        FileInfo file = new FileInfo(path);
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        con.Close();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           string p = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FileName"));

        }
    }
    
protected void  GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
        GridViewRow row = GridView1.SelectedRow;
}
protected void  GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        LinkButton lblProdId = (LinkButton)row.FindControl("FileName");
       string s=lblProdId.Text;  
        if (e.CommandName == "filedownload")
        {

            filedownload();
        }
    }
Posted
Updated 21-Apr-11 5:59am
v3
Comments
Sandeep Mewara 21-Apr-11 11:24am    
Ok, so what is the question/issue?
Albin Abel 21-Apr-11 12:02pm    
What you are trying to do is display the file in browser. Only supported files can be displayed. For simply downloading just a hyperlink of the virtual path of the file is fine. What exactly you are trying to do?
Prasanta_Prince 21-Apr-11 12:37pm    
What is your actual problem?
demouser743 22-Apr-11 5:18am    
May be you are missing response.flush() at the end
Steven.Pinto2000 29-Apr-11 6:33am    
Q is not clear where is the problem plz specify

I don't know whether your problem is solved or still there as it is an old question.

As I was googling, found this question.

I have written one Tip for Downloading files from GridView row, please take a look.
That will help you.

File download from GridView rows in ASP.NET 4.0[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900