Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my database contains following feilds:
SQL
autoid	        int	
pcode	    varchar(50)	
fyyear	    varchar(50)	
date	    varchar(50)	
salary	        int	
ta	        int	
contigency	int	
nrc	        int	
institcharges	int	
others	        int	
docname	    varchar(50)	
doctype	    varchar(50)	
docdata	    varbinary(MAX)

This is the code i am using to display the data in the button field:
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Document Download")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string url = GridView1.Rows[index].Cells[13].Text;
            System.IO.FileInfo file = new System.IO.FileInfo(url);
            if (file.Exists)
            {
                Response.Clear();
                Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
                Response.AppendHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.TransmitFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("NO FILE PRESENT");
            }
        }
    }

but when i click the button field nothing happens
note: i have linked my button field to docname data field.
Posted
Updated 4-Feb-13 20:14pm
v3
Comments
Sanjay K. Gupta 5-Feb-13 2:17am    
add break point at
Response.TransmitFile(file.FullName);
and see the file.FullName value
a2ulthakur 5-Feb-13 2:22am    
nothing in there ...i tried the breakpoints ..i just want a simple thing i want to download the file by its name (docname)from the button field in gridview my file is stored in database but i m not able to download it.

There is two ways:
1st:
C#
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[13].Text;
Response.Redirect(url)

And
2nd:
C#
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[13].Text;
FileInfo file = new FileInfo(url);
if (file.Exists)
{
   Response.Clear();
   Response.ClearHeaders();
   Response.ClearContent();
   Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
   Response.ContentType = "application/octet-stream";
   Response.AddHeader("Content-Length", file.Length.ToString());
   Response.WriteFile(file.FullName);
   Response.End();
}
else
{
   Response.Write("This file does not exist.");
}


Hope it works!
--Amit
 
Share this answer
 
Comments
a2ulthakur 5-Feb-13 2:27am    
string url = GridView1.Rows[index].Cells[13].Text; //in this line how do i identify cell 13. i wanna ask what does it signify ?is it the value of the cell where download button shd be ?
_Amy 5-Feb-13 2:32am    
Cell 13 will contain the URL of the files which you want to download. The value of Cell 13 should be like:
~/SomeFolder/Somefile.docx
a2ulthakur 5-Feb-13 2:39am    
but my files are on database ...they are not in any folder
a2ulthakur 5-Feb-13 4:08am    
Could not find a part of the path 'C:\Users\cgrt\Desktop\MIS\~C\Desktop\MIS\docs\'. this is the error i m getting at this line

Response.TransmitFile(Server.MapPath("~C/Desktop/MIS/docs/" + fileName));
Response.TransmitFile(Server.MapPath("~C/Desktop/MIS/docs/" + fileName));
i m getting error here "Could not find a part of the path 'C:\Users\cgrt\Desktop\MIS\~C\Desktop\MIS\docs\'."
 
Share this answer
 
Comments
boogac 5-Feb-13 9:59am    
when you write the full path ~ is needed ??

anyway,this is not an solution
a2ulthakur 5-Feb-13 13:42pm    
ya i know this is not the solution.

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